理解d写一次类型
有时,循环内设置常值,有时可抽象为λ,但在静态环境中不行.且置值时,我还要处理,而不是直接返回,但这又有另一个循环.
用例是,我的类型集中,有个常属性.要验证他们都相同,并取出其值.
是否有人有写一次类型,即允许仅初化一次类型,就像初化值一样,运行时决定是否置该类型.
类似可重绑定/尾常,头不必为常,只是需要复制进常引用
struct WriteOnce(T) {
const T payload;
const bool isSet;
void opAssign(U : T)(U data)
in (!isSet){
assert(!isSet);
@trusted() {
*(cast()&payload) = data;
*(cast()&isSet) = true;
}();
}
}
//上为hs.t
struct ColumnDef(T)//列类型
{
const TableDef table;//表,源,常
ExprString expr;//描述表达式,
alias type = T;
}
//计算列,定义1个从其他列计算出的类型的来定义列.相关了.
ColumnDef!T exprCol(T, Args...)(Args args)
{
//先找所有列,确保定义都来自该表
//列不能有多个表
const(TableDef)* tabledef;
foreach(ref arg; args)
{
static if(is(typeof(arg) == ColumnDef!U, U))
{
if(tabledef && arg.table != *tabledef)
throw new Exception("式中不能定义多表");
else
tabledef = &arg.table;
}
}
assert(tabledef !is null);
//构建表达式串
ExprString expr;
foreach(ref a; args)
{
static if(is(typeof(a) == string))
expr ~= a;
else
expr ~= a.expr;
}
return ColumnDef!(T)(*tabledef, expr);
//用的是指针抽象.
}
浙公网安备 33010602011771号