-
-
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
allow tuple destructuring in formal arguments #6614
Comments
+1 |
+1 |
Is this a general do block thing, or is it specific to map? Python has a starmap to do this, in Julia it would look a bit like:
And solves the problem:
If do blocks had special support for unpacking tuples then it would be inconsistent and confusing when the user changed their code from using a do block to passing in a function and found that they now had to unpack their tuples manually again. Obviously |
|
Yes, it should be possible to support the syntax |
There are enough other functions that apply a function argument to an iterator ( map(x) do splat(k, v) ... end as sugar for map(x) do x splat(x) do k, v ... end end But maybe that's too confusing, because that syntax could also mean: map(x) do
splat(k, v)
end A macro ( |
Another possibility is to automatically split the tuple with no new syntax, if the function is being passed a single tuple argument but the do block takes multiple arguments. That wouldn't be consistent for the case of a tuple of length 1, but I'm not sure that's a big deal. Not sure how this would be implemented, though. |
I feel that implicitly splatting things in some cases would be the Path of Madness. |
If this is useful enough that we are okay with extending the language for it, maybe just |
That's not a bad syntax. What's a little strange about introducing this is that it makes the do-block syntax(es) more flexible than the immediate lambda syntax, since there's no way to splat a single argument into multiple locals. Something like |
The real way to do this is to allow tuple expressions as formal arguments. Example:
would mean
Types would be specified as normal, e.g. |
I guess if you want to be fancy |
This is basically converging with pattern matching at this point (which is a good thing IMO). |
+1 for some variant of these ideas. |
Tuple destructuring in function arguments is a feature I've already missed several times, and given that function arguments are essentially the left-hand side of an assignment it seems only consistent if this would work. Luckily, it is not too hard to write a macro for this:
This essentially allows for the notation proposed by JeffBezanson:
|
I'm broadening this issue to "more general destructuring" and leaving it open for more design. |
+1 Elixir has me spoiled! It would be great to see pattern matching available on any kind of assignment. |
+1 |
Please don't chime in on issues just to say "+1" – it's extremely annoying for people who read each and every single notification on this repo. This is why GitHub implemented reactions – if you must |
Added by #23337 |
RFC: implement #6614, destructuring in formal arguments
While this works "typically," it doesn't seem to work for julia> mktemp() do path, io
redirect_stdout(io) do
println("Hello, world!")
end
end but just putting parentheses around the outputs of julia> mktemp() do (path, io)
redirect_stdout(io) do
println("Hello, world!")
end
end
ERROR: MethodError: no method matching (::getfield(Main, Symbol("##7#9")))(::String, ::IOStream)
Closest candidates are:
#7(::Any) at REPL[1]:2
Stacktrace:
[1] mktemp(::getfield(Main, Symbol("##7#9")), ::String) at ./file.jl:574
[2] mktemp(::Function) at ./file.jl:572
[3] top-level scope at none:0 |
I agree that the error message is super confusing, but that is actually the symptom of |
My impression is that Jeff felt this had been implemented in #23337. |
I don't see what isn't working, beyond the error being opaque. |
Aha: |
I've seen this a few times, maybe support could be added:
Right now people fall back on:
The text was updated successfully, but these errors were encountered: