# Using the @memoize macro

**URL:** <https://discourse.julialang.org/t/using-the-memoize-macro/39992>\
**Category:** General Usage\
**Created:** [May 22, 2020, 8:48pm UTC](https://discourse.julialang.org/t/using-the-memoize-macro/39992 "2020-05-22T20:48:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nimpa](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nimpa](https://discourse.julialang.org/u/Nimpa)\
**Post date:** [May 22, 2020, 8:48pm UTC](https://discourse.julialang.org/t/using-the-memoize-macro/39992/1 "2020-05-22T20:48:10Z")

</div>

```julia
macro memoize(fun::Expr)
local call = fun.args[1]
local name = call.args[1].args[1]
local arg1 = call.args[1].args[2]
local arg1_name = arg1.args[1]
local arg1_type = arg1.args[2]
local return_type = call.args[2]
local body = fun.args[2]
    quote
let cache = Dict{$(esc(arg1_type)), $(esc(return_type))}()
global function $(esc(name))($(esc(arg1_name))::$(esc(arg1_type)))::$(esc(return_type))
if haskey(cache, $(esc(arg1_name)))
cache[$(esc(arg1_name))]
else
cache[$(esc(arg1_name))] = $(esc(body))
end
end
end
end
end

```

Hello. Am trying to modify the above code to solve this problem  
##To generalize the @memoize macro and make it more robust (still for functions  
with a single argument) by checking whether the argument and return types  
are specified or not. If they are not, use the Any type instead of the argument or  
return type (or both) in the Dict that acts as the cache.

---

<div class="post-metadata">

**Author:** ![Duane\_Wilson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/duane_wilson/32/9506_2.png) [@Duane\_Wilson](https://discourse.julialang.org/u/Duane_Wilson)\
**Post date:** [May 24, 2020, 12:07am UTC](https://discourse.julialang.org/t/using-the-memoize-macro/39992/2 "2020-05-24T00:07:40Z")

</div>

Have you tried Memoize.jl?

> **[GitHub - JuliaCollections/Memoize.jl: ＠memoize macro for Julia](https://github.com/JuliaCollections/Memoize.jl)**
>
> ＠memoize macro for Julia. Contribute to JuliaCollections/Memoize.jl development by creating an account on GitHub.
