Currently, std.functional.memoize uses std.traits.ReturnType and std.traits.Parameters to get the return type and parameters of the memoized function. This fails for stuff like memoize!(a => a.field). Suggested new implementation:
template memoize(alias fun) {
auto memoize(Args...)(Args args) if (is(typeof(fun(args)))) {
import std.typecons : Tuple;
static if (__traits(isTemplate, fun)) {
import std.traits : isDelegate;
static assert(!isDelegate!(fun!Args), fun.stringof~" is a delegate, and thus has context memoize can't access.");
}
static typeof(fun(args))[Tuple!Args] memo;
auto t = Tuple!Args(args);
if (auto p = t in memo)
return *p;
return memo[t] = fun(args);
}
}
Comment #1 by dlang-bot — 2020-05-29T19:25:09Z
@Biotronic created dlang/phobos pull request #7507 "Fix issue 20099 - Memoize should handle lambdas" fixing this issue:
- Fix issue 20099
https://github.com/dlang/phobos/pull/7507
Comment #2 by robert.schadek — 2024-12-01T16:35:19Z