-
-
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
Stricter tuple destructuring. Error on (a, b) = (1, 2, 3)
#37132
Comments
Related |
I agree that this was probably a misfeature. |
If this is changed, should the same rule also apply to arrays (or anything supporting |
That does seem to be what's implied. So you iterate to destructure, then assert that the iterator is empty. The LHS syntax |
We could make it so that this at least parses in 1.x, right? julia> :((a,b, ...) = c)
ERROR: syntax: invalid identifier name "..." |
If it parses then giving it a new meaning is breaking, so we should not do that unless we give it the meaning that we ultimately want it to have. If the idea is to allow |
The current behavior makes it possible to add extra output arguments to a function in the future without breaking callers. I don't see the value of forcing callers to acknowledge extra elements they are not interested in. |
The trouble is that adding extra return values still isn't non-braking because people might do things like apply |
Yes, but this change would strictly increase the potential for breakage. |
True, but since something is either breaking or not I'm not sure that helps that much. Would we generally add return values from Base functions? |
I could be off base, but it seems reasonable to think of this feature as the return-counterpart of optional positional arguments. if I have a function
I can later define
and it will be a non-breaking change. (It's not totally correct, since multiple return values are "mechanically" the counterpart to argument destructuring julia> h((a,b)) = a + b
h (generic function with 1 method)
julia> h((1,2))
3
julia> h((1, 2, 3))
3 ) I don't know what conclusion that takes us to exactly, but it roughly appears that there is a conflict for what Does this feature in Julia have anything to do with For this "optional return values" feature to be safe to use, functions could return e.g. Finally, it seems like these features should compose in some way. I usually think of functions composing like In summary: Optional Positional Argument:
Optional Return Value:
|
a reasonable candidate for #54903 |
Current behavior
I think I'd like an error. This would be breaking on Julia 1.0, but @DilumAluthge suggested I open an issue to keep track of it possibly for Julia 2.0.
As mentioned in the manual, a
Tuple
can be thought of a positional-argument-only function call without the function. A function call binds values in the calling context to the formal arguments in the function body context, and so it could be nice to have tuple assignment behave more similarly to that binding process. In other words, you cannot callf(a, b)
asf(1, 2, 3)
.In other words still, if tuple destructuring were more strict, then the two functions
g
andh
would be equivalent (up to error type):h((2,3,4))
would throw some kind of error (notMethodError
).This is what happens today:
If the strict behavior is desired, I don't know whether to enforce it with a
length
call, or whether to more generally check that the iterator is empty at the end.The text was updated successfully, but these errors were encountered: