# @memoize macro

**URL:** https://discourse.julialang.org/t/memoize-macro/39968
**Category:** General Usage
**Tags:** question, memoize
**Created:** [May 22, 2020, 1:43pm UTC](https://discourse.julialang.org/t/memoize-macro/39968 "2020-05-22T13:43:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)
#### Post date: [May 22, 2020, 1:43pm UTC](https://discourse.julialang.org/t/memoize-macro/39968/1 "2020-05-22T13:43:24Z")

</div>

Hello. I am working on implement macro @memoize. it works well but i want to generalize it 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, I want to use the Any type instead of the argument in the Dict that acts as the cache. any help or tips?

```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

```

---

<div class="post-metadata">

### Author: ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)
#### Post date: [May 24, 2020, 11:55am UTC](https://discourse.julialang.org/t/memoize-macro/39968/2 "2020-05-24T11:55:24Z")

</div>

[GitHub - zgornel/Caching.jl: Memoization mechanism](https://github.com/zgornel/Caching.jl) may also be of help.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 24, 2020, 3:07pm UTC](https://discourse.julialang.org/t/memoize-macro/39968/3 "2020-05-24T15:07:52Z")

</div>

Is this different from `@memoize` in [Memoize.jl](https://github.com/JuliaCollections/Memoize.jl)?

---

<div class="post-metadata">

### Author: ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)
#### Post date: [May 26, 2020, 8:01am UTC](https://discourse.julialang.org/t/memoize-macro/39968/4 "2020-05-26T08:01:35Z")

</div>

Most probably 😉
