函数式编程陷入 D 困境

发布于 2024-11-25 17:10:43 字数 1304 浏览 0 评论 0原文

我在 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 无法推导出来自相当明确的参数类型的模板函数...

我做错了什么。

如果这应该在代码审查堆栈交换中而不是在这里,请告诉我,但我希望这是我对模板如何工作的误解,而不是错误。

I'm having trouble with creating a template in D that works:

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;
}

And then my unit-tests:

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);
}

which are failing with the following error:

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

I wouldn't be asking, however, I don't understand why D is having trouble deducing the template function from the rather explicit argument types...

What am I doing wrong.

If this should be in code review stack exchange rather than here, let me know, but I expect this to be a misunderstanding on my part about how templates work, rather than a bug.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里南柯 2024-12-02 17:10:43

我现在无法测试它,但我认为这是因为 in T[]scope const T[] ,它是 scope const(T[]),这对每个人来说都比scope const(T)[]更烦人,同时几乎没有任何好处。

尝试将参数列表中的更改

in T x, in T[] t

scope T x, scope const(T)[] t

,看看是否可以解决问题。

I can't test it right now, but I think it's because in T[] is scope const T[] which is scope const(T[]), which is a lot more annoying for everyone to deal with than scope const(T)[], and at the same time, has pretty much no benefits.

Try changing

in T x, in T[] t

to

scope T x, scope const(T)[] t

in the parameter list, to see if that solves the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文