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

Setting properties #90

Closed
cstjean opened this issue Oct 1, 2019 · 5 comments
Closed

Setting properties #90

cstjean opened this issue Oct 1, 2019 · 5 comments

Comments

@cstjean
Copy link

cstjean commented Oct 1, 2019

I'm interested in something like

@set object.property.x = 10

Can I support it? I get an error from assert_hasfields because, of course, property is not a field of object. I'd like to be able to implement setproperty(::Object, value, sym), similar to Base.setindex, which would return a new object with that property modified. Is that possible?

@jw3126
Copy link
Owner

jw3126 commented Oct 1, 2019

Yes! You need to overload Setfield.setproperties(obj, patch::NamedTuple). In the next few days/weeks this is moved to the tiny dependency ConstructionBase, see also JuliaObjects/ConstructionBase.jl#1
So in future you only need to depend on ConstructionBase and overload ConstructionBase.setproperties(obj, patch::NamedTuple)

@jw3126
Copy link
Owner

jw3126 commented Oct 1, 2019

See for an example here:

struct CustomProperties

@cstjean
Copy link
Author

cstjean commented Oct 1, 2019

It seems to work great, thank you!

@cstjean cstjean closed this as completed Oct 1, 2019
@cstjean
Copy link
Author

cstjean commented Oct 1, 2019

BTW, I haven't followed the entire discussion around constructor_of, but wouldn't it make sense to rewrite Setfield.jl around setproperties, with a fall-back to the constructor when it's not implemented? Relying on the constructor never felt quite right to me.

@jw3126
Copy link
Owner

jw3126 commented Oct 1, 2019

I would say that Setfield.jl is already designed around setproperties. The default implementation just uses constructor_of. Try

Setfield.setproperties(MyType, patch) = ...
Setfield.constructor_of(::Type{<:MyType}) = error()

And Setfield.jl should work fine with MyType.

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