[英]Functional programming in D trouble
我在D中创建有效的模板时遇到了麻烦:
pure T BSpline(int k:1, T)(in T x, in T[] t)
{
if (t[0] <= x && x < t[k])
return 1;
else
return 0;
}
pure T BSpline(int k, T)(in T x, in T[] t)
{
if (t[0] <= x && x < t[k])
{
T a = (x - t[0]) / (t[k-1] - t[0]);
T b = (t[k] - x) / (t[k] - t[1]);
return a * BSpline!(k-1,T)(x, t[0..k-1]) + b * BSpline!(k-1,T)(x, t[1..k]);
}
else
return 0;
}
然后我的单元测试:
unittest {
real a = .5;
real[] b = [0,0,1,2,3];
assert(BSpline!(1,real)(a,b[0..2]) == 0);
assert(BSpline!(2,real)(a,b[0..3]) == .5);
assert(BSpline!(3,real)(a,b[0..4]) == .625);
assert(BSpline!(4,real)(a,b[0..5]) == 0.260417);
}
失败并显示以下错误:
bspline.d(18): Error: template BSplineBasis.BSpline(int k : 1,T) does not match any function template declaration
bspline.d(18): Error: template BSplineBasis.BSpline(int k : 1,T) cannot deduce template function from argument types !(1,real)(const(real),const(real[]))
bspline.d(18): Error: template instance errors instantiating template
我不会问,但是,我不明白为什么D很难从相当明确的参数类型推导模板函数...
我究竟做错了什么。
如果应该在代码审阅堆栈交换中而不是在此处进行交换,请告诉我,但是我希望这对模板的工作方式造成误解,而不是错误。
我现在无法测试,但是我认为这是因为in T[]
是scope const T[]
,这是scope const(T[])
,这对每个人来说都比scope const(T)[]
,同时几乎没有任何好处。
尝试改变
in T x, in T[] t
至
scope T x, scope const(T)[] t
在参数列表中,查看是否可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.