memoize (is this a fix for overloading?)

%u wfunction at hotmail.com
Tue Jan 4 12:57:10 PST 2011


Hi all,

How does this work, with respect to overloading?

auto memo(alias Fn, TParams...)(TParams args) if (isCallable!(Fn))
{
    static typeof(Func(args))[Tuple!(TParams)] cache;
    auto key = tuple(args);
    return key in cache ? cache[key] : (cache[key] = Func(args));
}

uint fib(uint n)
{ return n > 1 ? memo!(fib)(n - 1) + memo!(fib)(n - 2) : n; }
ulong fib(ulong n)
{ return n > 1 ? memo!(fib)(n - 1) + memo!(fib)(n - 2) : n; }

void main() { memoize(&fib); std.stdio.writefln("%d", fib(50)); }


It seems like it's fixed, right?


More information about the Digitalmars-d mailing list