fonsp / Pluto.jl

🎈 Simple reactive notebooks for Julia
https://plutojl.org/
MIT License
4.91k stars 284 forks source link

error upon defining the functor Polynomial example from Julia docs #2735

Closed ggggggggg closed 7 months ago

ggggggggg commented 7 months ago

Video link: https://share.vidyard.com/watch/156vhpJQCmXUcbUbzm5QWq? Notebook Gist: https://gist.github.com/ggggggggg/08a49b61756565a1321b6f0df79d122f

I tried to define a functor following the julia docs here: https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects-1 Here is the full text of the example I tried to recreate:

julia> struct Polynomial{R}
           coeffs::Vector{R}
       end

julia> function (p::Polynomial)(x)
           v = p.coeffs[end]
           for i = (length(p.coeffs)-1):-1:1
               v = v*x + p.coeffs[i]
           end
           return v
       end

julia> (p::Polynomial)() = p(5)

I expected it to work, so I could for example call p(3), instead I get an error "Multiple definitions for Polynomial".

Pangoraw commented 7 months ago

Functors needs to currently be defined in the same cell as the type (struct Polynomial{R} in your case). See https://github.com/fonsp/Pluto.jl/issues/2038 which tracks the issue and has more context.