-
-
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
default values for destructuring syntax: (; x=default) = y #51940
Comments
I guess the issue here is that if we think of this syntax more in terms of pattern matching, one might expect this to instead unpack |
To me, in the context of Julia syntax it looks more like keyword arguments with default values, though of course this is a matter of taste. Moreover, it seems pretty useful since If you want to do renaming, it would seem more natural to mirror the |
One thing I like about the pattern matching approach is that it makes property destructuring nestable which it's currently not. I often find myself wanting to express something like There currently isn't really a way to neatly express this in one line, so I do think composability is one argument in favor of this approach over your proposal. Using the |
Recursive destructuring could instead use (; head, (_, a, b) = args) = :(1 + 2) which looks more natural to me, and is disambiguated from default values by the tuple on the lhs of the (; head, (_, a=nothing, b=nothing) = args) = some_expr |
It is disambiguous but I feel like this violates our unwritten syntax rules in terms of referential transparency. The rhs of the |
It is even weirder to have the rhs be the symbol you are assigning to, as in your suggestion. |
What about using another assignment operator for the default value ? (; a = new_name := default_value()) = some_expr
(; a := default_value()) = some_expr
(; a = new_name) = some_expr |
To me this reads more like unpacking Edit: I recognize that was already said ^^; |
As commented by @simeonschaub in #39285, it might be nice to have a way to provide default values in destructuring syntax. This came up recently on discourse.
A natural syntax for this might be:
where the default values are employed if
!hasproperty(x, :a)
and/or!hasproperty(x, :c)
.More precisely, I think it would be good to specify that the default-value expressions are only evaluated if the corresponding property on the rhs does not exist. So that you could do
(; a=default_a()) = x
anddefault_a()
would only be called ifx.a
does not exist, functionally equivalent toa = hasproperty(x, :a) ? x.a : default_a()
.The text was updated successfully, but these errors were encountered: