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 .
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 https://stackoverflow.com/questions/54023333/can-i-pass-keyword-args-to-do-blocks-in-julia .
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
bkamins
January 4, 2019, 10:08am
#9
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