From 09b83486a19f0e4fe825f2e0b4f013f38279c1ee Mon Sep 17 00:00:00 2001 From: cstjean Date: Wed, 28 Oct 2020 13:25:42 -0400 Subject: [PATCH] Get rid of eval Fix #48, at the cost of putting the variable in a poorly-performing global. Not sure if this is acceptable. It's frustrating that Julia seemingly lacks the tools to deal with this elegantly. - If `const` worked in a local function, we'd just put `const` and be done with it. - If `typeconst` existed for global variables, that would work too. Memoization.jl uses generated functions, which causes other problems. And it feels like the wrong solution too. --- src/Memoize.jl | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Memoize.jl b/src/Memoize.jl index 8e8d6d2..475505e 100644 --- a/src/Memoize.jl +++ b/src/Memoize.jl @@ -51,23 +51,22 @@ macro memoize(args...) fcachename = Symbol("##", f, "_memoized_cache") mod = __module__ - fcache = isdefined(mod, fcachename) ? - getfield(mod, fcachename) : - Core.eval(mod, :(const $fcachename = $cache_dict)) if length(kws) == 0 - lookup = :($fcache[($(tup...),)]::Core.Compiler.return_type($u, typeof(($(identargs...),)))) + lookup = :($fcachename[($(tup...),)]::Core.Compiler.return_type($u, typeof(($(identargs...),)))) else - lookup = :($fcache[($(tup...),)]) + lookup = :($fcachename[($(tup...),)]) end def_dict[:body] = quote - haskey($fcache, ($(tup...),)) ? $lookup : - ($fcache[($(tup...),)] = $u($(identargs...),; $(identkws...))) + haskey($fcachename, ($(tup...),)) ? $lookup : + ($fcachename[($(tup...),)] = $u($(identargs...),; $(identkws...))) end esc(quote + $fcachename = $cache_dict # this should be `const` for performance, but then this + # fails the local-function cache test. $(combinedef(def_dict_unmemoized)) - empty!($fcache) + empty!($fcachename) Base.@__doc__ $(combinedef(def_dict)) end)