JuliaCollections / Memoize.jl

@memoize macro for Julia
Other
177 stars 22 forks source link

Memoize does not work in 0.5 when adding 2 methods to a function #5

Closed aviks closed 8 years ago

aviks commented 8 years ago

In Julia 0.5

julia> @memoize foo() = 2
(::#33#foo) (generic function with 1 method)

julia> @memoize foo(x) = 3
ERROR: cannot define function foo; it already has a value
 in anonymous at /Users/aviks/.julia/v0.5/Memoize/src/Memoize.jl:103
 in eval(::Module, ::Any) at ./boot.jl:267

This works OK in v0.4

julia> @memoize foo() = 2

julia> @memoize foo(x) = 3

julia> foo()
2

julia> foo(1)
3

cc: @jeffbezanson : I presume this is due to the function rework. Is there an easy workaround that comes to mind?

aviks commented 8 years ago
julia> macroexpand(:(@memoize foo(x) = 3))
quote  # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 92:
    foo_unmemoized(x) = begin  # none, line 1:
            3
        end # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 93:
    #545#isdef = false # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 94:
    try  # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 95:
        foo # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 96:
        #545#isdef = true
    catch 
    end # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 98:
    if #545#isdef # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 99:
        for #546#i = 1 # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 100:
            begin  # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 101:
                local fcache # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 102:
                const fcache = ObjectIdDict() # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 103:
                foo(x) = begin  # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 103:
                        if haskey(fcache,(x,))
                            fcache[(x,)]
                        else 
                            fcache[(x,)] = foo_unmemoized(x)
                        end
                    end
            end
        end
    else  # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 109:
        begin  # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 110:
            const foo = let  # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 111:
                        local fcache, foo # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 112:
                        const fcache = ObjectIdDict() # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 113:
                        foo(x) = begin  # /Users/aviks/.julia/v0.4/Memoize/src/Memoize.jl, line 113:
                                if haskey(fcache,(x,))
                                    fcache[(x,)]
                                else 
                                    fcache[(x,)] = foo_unmemoized(x)
                                end
                            end
                    end
        end
    end
end
aviks commented 8 years ago

This looks to have been fixed in base julia, and now works at least as of Version 0.5.0-dev+3660 (2016-04-20 14:02 UTC).

nkottary commented 8 years ago

This bug occurs in latest Julia Version 0.5.0-dev+4987 Commit 3ac1e62* (2016-06-28 18:44 UTC)

aviks commented 8 years ago

Yes, I was just searching for this issue to say the same thing. Ah well.

simonster commented 8 years ago

The obvious way to fix this does not work (JuliaLang/julia#18672)