Allowing for keyword arguments in do-block

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

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).

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

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.

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: