Issue 20099 - Memoize should handle lambdas
Summary: Memoize should handle lambdas
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P4 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2019-08-02 22:50 UTC by Simen Kjaeraas
Modified: 2024-12-01 16:35 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2019-08-02 22:50:17 UTC
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 Dlang Bot 2020-05-29 19:25:09 UTC
@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 dlangBugzillaToGithub 2024-12-01 16:35:19 UTC
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