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

unpack with @with_kw #123

Open
joaogoliveira1 opened this issue May 21, 2020 · 5 comments
Open

unpack with @with_kw #123

joaogoliveira1 opened this issue May 21, 2020 · 5 comments

Comments

@joaogoliveira1
Copy link
Contributor

Hello,

I was trying to run the example in the manual section "(Un)pack macros":

@with_kw struct Para{R<:Real}
    a::R = 5
    b::R
    c::R = a+b
end

pa = Para(b=7)

function fn2(var, pa::Para)
    @unpack a, b = pa
    out = var + a + b
    b = 77
    @pack! pa= b 
    return out, pa
end

I get the following error:

setfield! immutable struct of type Para cannot be changed
setproperty! at Base.jl:34 [inlined]
pack! at UnPack.jl:61 [inlined]
macro expansion at UnPack.jl:152 [inlined]
fn2(::Int64, ::Para{Int64}) at scratch.jl:61
top-level scope at scratch.jl:65

Has the package been update in a way that this example is no longer valid? I could really use this feature of changing the values of an instance of parameters. Thanks.

@mauro3
Copy link
Owner

mauro3 commented May 21, 2020

Yes, that's a mistake in the docs, packing only works with mutable structs. Alternativvely you could use Setfield.jl. Fancy submitting a PR to fix the docs?

@joaogoliveira1
Copy link
Contributor Author

I'm new to Github. Should the pull request eliminate the reference to unpack in this case?

@joaogoliveira1
Copy link
Contributor Author

Maybe use Setfield in this instance, instead of @pack! ?

@mauro3
Copy link
Owner

mauro3 commented May 22, 2020

Maybe make a mutable struct:

@with_kw mutable struct MPara{R<:Real}
....

Also, maybe add a note that @pack! only works with mutables.

@joaogoliveira1
Copy link
Contributor Author

Done!

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

2 participants