Bug 20099 – Memoize should handle lambdas

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2019-08-02T22:50:17Z
Last change time
2024-12-01T16:35:19Z
Keywords
pull
Assigned to
No Owner
Creator
Simen Kjaeraas
Moved to GitHub: phobos#10380 →

Comments

Comment #0 by simen.kjaras — 2019-08-02T22:50:17Z
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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10380 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB