Allowing for keyword arguments in do-block

proposal

#1

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.


#2

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

How about , instead of ;?

f(args) do x, y, kw=1

#4

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


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

#6

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.


#7

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)


#8

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.


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