我正在创建一个输入范围来遍历自定义容器,该容器包含需要保持精确配对的输入和目标的数据点。我需要不同的范围来返回训练数据(double [] []),输入(double [])和目标(也是double [])。我设法得到以下代码进行编译和完美工作,但我不知道为什么。结构表达式参数与类型参数
public struct DataRange(string type)
if(type == "TrainingData" ||
type == "InputData" ||
type == "TargetData")
{
private immutable(int) length;
private uint next;
private Data data;
this(Data d){
this.length = d.numPoints;
this.next = 0;
this.data = d;
}
@property bool empty(){return next == length;}
@property auto front(){
static if(type == "TrainingData")
return this.data.getTrainingData(next);
else static if(type == "InputData")
return this.data.getInputData(next);
else return this.data.getTargetData(next);
}
void popFront(){++next;}
}
static assert(isInputRange!(DataRange!"TrainingData"));
static assert(isInputRange!(DataRange!"InputData"));
static assert(isInputRange!(DataRange!"TargetData"));
我一直在读了“d编程语言”,由Alexandrescu的,我已经找到了形式
struct S(T){...} // or
struct S(T[]){...}
但这些都需要类型参数,而不是表达我好象的参数结构完成。我无法在参数化类型dlang.org上找到任何类似的例子。
这个编译和工作在DMD 2.066和GDC 4.9.0。
我甚至不知道我为什么试过这个,回头看看它,我不知道它为什么起作用。有人知道我错过了什么吗?这在哪里记录?
有一本关于D模板的书籍/教程可在线获取:https://github.com/PhilippeSigaud/D-templates-tutorial。它有时可以比语言文档更好地解释事物。 – yaz 2014-10-17 08:34:47