Generic structural recursion

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Jan 26 19:57:19 UTC 2022


On Wed, Jan 26, 2022 at 02:06:18PM -0500, Steven Schveighoffer via Digitalmars-d wrote:
[...]
> ```d
> auto interpolate(alias fn, X : double)(ref X val1, ref X val2)
> {
>    static if(is(typeof(fn(val1, val2)))) return fn(val1, val2);
> }
> 
> auto interpolate(alias fn, X)(ref X val1, ref X val2) if (is(X == struct))
> {
>    // loop and recurse
> }
> ...
> ```

That's what I had, but the problem is that the number of X arguments
differs from function to function.  So I'd have to duplicate the
recursion part for each number of arguments, which is bad because
there's a chance I might change the recursion in the unary version and
forget to update it in the binary/ternary/etc. version.


T

-- 
Why can't you just be a nonconformist like everyone else? -- YHL


More information about the Digitalmars-d mailing list