-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
keyword argument constructor for immutables #5333
Comments
I like this. If we're going to do something like this, could we also consider having both mutable and immutables types allow default values, which would allow the use of keyword arguments for construction from scratch (rather than based on a small variant of an existing immutable)? When writing my Vega wrapper I wound up writing up some hacky code to allow type fields to have default values and would love to have something more official. |
Even better:
|
I really like that last version. The business with |
Relatedly it might make sense to have nice to have syntax sugar for modifying the field of an immutable when placed in an array. |
It would also be nice to not have to write down the type. In haskell it would be |
Yes, we dislike that idea since it means |
For example, the type immutable X a::Int b::Int end now comes with the extra constructor: X(x::X; a=x.a, b=x.b) = new(a,b) Closes #5333.
|
I think we need to start thinking about syntax for modifying immutables in place in arrays (and potentially in other mutables) now that that tuples can be used as fixed size arrays. Right now using them that way is quite painful. |
Bump |
For example, the type immutable X a::Int b::Int end now comes with the extra constructor: X(x::X; a=x.a, b=x.b) = new(a,b) Closes #5333.
For example, the type immutable X a::Int b::Int end now comes with the extra constructor: X(x::X; a=x.a, b=x.b) = new(a,b) Closes #5333.
It seems the above commit (9233943) is not in the current |
See #6122 |
I think we still want this – at least I periodically do and then wonder why we don't have it yet. |
I don't believe this would be breaking, would it? |
Wouldn't it break |
Base now has |
@JeffBezanson and I were discussing adding a second default constructor with the semantics
This would cover the case where you want to obtain a new value with one field changed e.g. in the example above
This is especially useful when the immutable has a lot of field. One example that comes to mind and where I already define a constructor like the above is URIParser.jl
The text was updated successfully, but these errors were encountered: