d用联还是用变量
enum F_Type{
CONS,
STRN,
NMBR,
EROR,
BOOL,
FUNC,
}
struct Atom{
F_Type kind;
Atom* car;
Atom* cdr;
double num;
string str;
bool bul;
F_Error err = F_Error.NOVALUE;
}
已跟踪数据类型,用联.
struct Atom {
F_Type kind;
union {
Atom* car;
Atom* cdr;
double num;
string str;
bool bul;
F_Error err = F_Error.NOVALUE;
}
}
一般,我建议std.sumtype.它按包含构/所有要求类型的联两个字段实现.
即,你可能面临重构代码来使用匹配(match)和试匹配(tryMatch)函数,因为std.sumtype.SumType不会公开底层种类字段.
有几个字段需要共存.需要{car,cdr}或{num}或{str}或{bul}.
由于这是动态类型化语言,我需要原子既可以互换,又可以同时服务不同目的.
只需要构建嵌套匿名构:
struct Atom {
F_Type kind;
union {
struct {
Atom* car;
Atom* cdr;
}
struct {
double num;
string str;
}
bool bul;
}
F_Error err = F_Error.NOVALUE;
//再添加了构造器.
this( double n ){ kind = F_Type.NMBR; num = n; } // make number
this( string s ){ kind = F_Type.STRN; str = s; } // make string
this( bool b ){ kind = F_Type.BOOL; bul = b; } // make bool
this( Atom* a, Atom* d ){ kind = F_Type.CONS; car = a; cdr = d; } // make cons
this( F_Error e, string m ){ kind = F_Type.EROR; err = e; msg = m; } // make error
}
浙公网安备 33010602011771号