d模块实现多态

原文
我想到了两种可能性.一个是使用UFCS,定义类型作为第一个参数其他模块命名函数(它不适合操作符重载),然后可用点语法调用:

module myvector;
struct vector {
     float x;
     float y;
}
//
module myvectormath;
import myvector;
vector add(vector lhs, vector rhs) {
     // 内部是普通函数.
     vector result;
     result.x = lhs.x + rhs.x;
     result.y = lhs.y + rhs.y;
     return result;
}

用法:

import myvector;
import myvectormath;

// 可用.号来调用
vector a = vector(0,0).add(vector(5, 5));

另一种可能的方法是把数据放入结构或插件模板中,然后通过放其入具有所需功能的另一个结构中来计算数学:

// 数据
module myvector;

// 外部命名类型,
struct VectorData {
   float x;
   float y;
}

// 准备
mixin template vector_payload() {
// 构造器,方便初化
this(float x, float y) {
    _data.x = x;
    _data.y = y;
}
this(VectorData d) {
    _data = d;
}

    // 存储数据
VectorData _data;
alias _data this;//隐式转换.
}

// 数学#1模块
module myvectormath;
import myvector;

struct vector {
    // 插件数据
    mixin vector_payload!();

// 加方法,重载操作符
    vector opBinary(string op:"+")(vector rhs) {
        vector result;
        result.x = this.x + rhs.x;
        result.y = this.y + rhs.y;
        return result;
    }
}

// #2模块
module myvectormath2;
import myvector;

struct vector {
    // 插件
    mixin vector_payload!();

// 加方法
    vector opBinary(string op:"+")(vector rhs) {
        vector result;
    // 完全不一样
        result.x = this.x - rhs.x;
        result.y = this.y - rhs.y;
        return result;
    }
}

// 用法 
import myvectormath;
// 或
//import myvectormath2;
void main() {
    vector a = vector(0, 0) + vector(5, 5);
    import std.stdio;
    writeln(a);
}

如何同时使用?这就是内部结构_Data,构造器和别名的用武之地.
因此,首先,要消除名字歧义.现在,只使用全名.

// 用法
import myvectormath;
import myvectormath2;
void main() {
    // 指定类型
    myvectormath.vector a = myvectormath.vector(0, 0) + myvectormath.vector(5, 5);
    import std.stdio;
    writeln(a); // 正确
}

怎样才能轻松地在内部移动它们?创建使用#2版本的函数:

void somethingWithMath2(myvectormath2.vector vec) {
// ...
}

如果传递"a"变量给它,它会抱怨,因为它是myvectormath.vector,而这是myvectormath2.
但是,可很容易地转换它们,这要归功于mixin模板中的外部数据结构,ctor和别名:

somethingWithMath2(myvectormath2.vector(a));

编译!幕后工作是myvectormath2.vector有两个构造器:(float,float)(VectorData).它们都不匹配a类型,所以接着它会别名本VectorData.所以它隐式转换然后匹配VectorDatactor.也可只传递数据:

import myvector;
void somethingWithMath2(VectorData a_in) {
// 运算
auto a = myvectormath2.vector(a_in);
// 使用
}
//调用
somethingWithMath2(a);

传递数据可能是最好的.

posted @   zjh6  阅读(11)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示