I wanted to ask if it was considered to add support for keyword arguments in do-block statement?
So that something like:
fun(args; kwargs) do (args2; kwargs2)
#body
end
would be rewritten as:
fun(function(args2;kwargs2)
#body
end, args; kwargs)
The origin of the proposal is here .
1 Like
That syntax wouldn’t really be consistent, since
f(args) do (x,y)
#body
end
gives an anonymous function which takes a single tuple as an argument, i.e. is
f(function ((x,y),)
#body
end, args)
The way to specify a multiple argument function is without parentheses, i.e.
f(args) do x,y
#body
end
Unfortunately using a semicolon as a keyword separator won’t work, as it is already used as an expression delimiter, i.e.
f(args) do x,y; kw=1
#body
end
is equivalent to
f(function (x,y)
kw = 1
#body
end, args)
3 Likes
simonbyrne:
f(args) do x,y; kw=1
How about ,
instead of ;
?
f(args) do x, y, kw=1
1 Like
That’s currently a syntax error (so is available), but would also be inconsistent since the do
syntax is used for function definition (in function definitions, a=b
args are default values for positional arguments).
Azamat
January 4, 2019, 12:57am
5
How about parsing
map(args) do x, y=1
#body
end
as the function with default values for positional arguments:
map(function (x, y=1)
#body
end, args)
What would be the point? Presumably map
would supply arguments which are congruent, so you would either always get an y
, or never do (so just use a constant).
@bkamins : Can you give an example where you would find keyword args useful? I would consider more than 2 (or at most 3, but that’s really pushing it) args for a function I use in a do
block code smell.
I asked following the question in Can I pass keyword args to do-blocks in julia? - Stack Overflow .
But the example could be (I am not advocating to add it, but just giving an example):
map(round, x, digits=3)
as an equivalent of
round.(x, digits=3)
(the general pattern could be the case when the caller function passes through its keyword arguments to a called function)
Thanks. I understand you are not advocating this, but I think that
bkamins:
map(round, x, digits=3)
as an equivalent of
round.(x, digits=3)
would be very confusing, as arguments would end up mapped or not based on being keyword or positional.
1 Like
Yes - it was only an example of a function we have in Base already.
Note, however, that in broadcasting keyword arguments are exactly not broadcasted over and I guess people expect this behavior.
1 Like
Is this behaviour of the do block for multiple arguments, etc. documented anywhere? I didn’t see it in the tutorial: Functions · The Julia Language .
o314
August 13, 2024, 1:51am
11
Another solution :
Handle a special mode (breathe deeply and enjoy - without any parenthesis juggling) in the parser at the do form level.
Most frequently the do form expects quickly a line break after the statement of the args.
One can capture there the first ‘;’ before first NL, its left side, and give it back as a passing of some named args.
Some work for @c42f ?
SEE ALSO
I think this is what you looking for (Maybe)… I use a small trick to encapsulate the keyword argument via Julia’s Do-Block arguments.
Please refer to the Link for details:
opened 12:10PM - 03 Jul 23 UTC
parser
feature
I could not find out how to use do-blocks with keyword arguments, so either it i… s not possible or underdocumented. Cf
```julia
just_call(f, args...; kwargs...) = f(args...; kwargs...)
just_call((x; z) -> x + z, 1; z = 2)
```
and the "logical" do-block version would be
```julia
just_call(1; z = 2) do x; z
x + z
end
```
which fails with
```julia
ERROR: MethodError: no method matching (::var"#17#18")(::Int64; z::Int64)
Closest candidates are:
(::var"#17#18")(::Any) got unsupported keyword argument "z"
@ Main REPL[34]:1
```
(FWIW, this is on Julia 1.9.1).
Cf [discourse](https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111), I could not find an open issue.
opened 12:10PM - 03 Jul 23 UTC
parser
feature
I could not find out how to use do-blocks with keyword arguments, so either it i… s not possible or underdocumented. Cf
```julia
just_call(f, args...; kwargs...) = f(args...; kwargs...)
just_call((x; z) -> x + z, 1; z = 2)
```
and the "logical" do-block version would be
```julia
just_call(1; z = 2) do x; z
x + z
end
```
which fails with
```julia
ERROR: MethodError: no method matching (::var"#17#18")(::Int64; z::Int64)
Closest candidates are:
(::var"#17#18")(::Any) got unsupported keyword argument "z"
@ Main REPL[34]:1
```
(FWIW, this is on Julia 1.9.1).
Cf [discourse](https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111), I could not find an open issue.