Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize cannot be used on functions defined on instances of a struct #78

Open
kunzaatko opened this issue Nov 1, 2022 · 4 comments
Open

Comments

@kunzaatko
Copy link

Great package! I have one slight problem. It is currently impossible to @memoize functions that do not have conventional names but are instead defined on some types. For example:

  julia> using Memoize
  julia> struct Some
             x::Number
         end
  julia> @memoize function (s::Some)(y,z)
             s.x * y * z
         end
  julia> some = Some(10)
  > Some(10)
  julia> some(60,90)
  ERROR: UndefVarError: s not defined
  Stacktrace:
   [1] var"##s::Some_unmemoized"(y::Int64, z::Int64)
     @ Main ./REPL[3]:2
   [2] #1
     @ ~/.julia/packages/Memoize/12ANR/src/Memoize.jl:62 [inlined]
   [3] get!
     @ ./iddict.jl:178 [inlined]
   [4] (::Some)(y::Int64, z::Int64)
     @ Main ~/.julia/packages/Memoize/12ANR/src/Memoize.jl:61
   [5] top-level scope
     @ REPL[5]:1

Can you think of a way to solve this?

@cstjean
Copy link
Collaborator

cstjean commented Nov 1, 2022

With a pull request! This shouldn't be too difficult, so if you're up for macros, I would encourage you to try.

Otherwise... On the user side, you could do this:

(s::Some)(y,z) = call_some(s,y,z)

then memoize call_some.

@kunzaatko
Copy link
Author

I am not so comfortable with macros, but I can try it out.

@kunzaatko
Copy link
Author

(s::Some)(y,z) = call_some(s,y,z)

I thought of this but thought it would be nice for it to be supported in some other way :)

@marius311
Copy link

Fyi https://github.com/marius311/Memoization.jl/ has this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants