# Allowing for keyword arguments in do-block

**URL:** <https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244>\
**Category:** Internals & Design\
**Tags:** proposal\
**Created:** [January 3, 2019, 6:51pm UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244 "2019-01-03T18:51:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 3, 2019, 6:51pm UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/1 "2019-01-03T18:51:34Z")

</div>

I wanted to ask if it was considered to add support for keyword arguments in do-block statement?  
So that something like:

```julia
fun(args; kwargs) do (args2; kwargs2)
    #body
end

```

would be rewritten as:

```julia
fun(function(args2;kwargs2)
    #body
end, args; kwargs)

```

The origin of the proposal is [here](https://stackoverflow.com/questions/54023333/can-i-pass-keyword-args-to-do-blocks-in-julia).

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [January 3, 2019, 7:17pm UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/2 "2019-01-03T19:17:29Z")

</div>

That syntax wouldn’t really be consistent, since

```julia
f(args) do (x,y)
    #body
end

```

gives an anonymous function which takes a single tuple as an argument, i.e. is

```julia
f(function ((x,y),)
    #body
end, args)

```

The way to specify a multiple argument function is without parentheses, i.e.

```julia
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.

```julia
f(args) do x,y; kw=1
   #body
end

```

is equivalent to

```julia
f(function (x,y)
    kw = 1
    #body 
end, args)

```

---

<div class="post-metadata">

**Author:** ![anon43133060](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon43133060](https://discourse.julialang.org/u/anon43133060)\
**Post date:** [January 3, 2019, 9:46pm UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/3 "2019-01-03T21:46:50Z")

</div>

> [@simonbyrne](#):
>
> f(args) do x,y; kw=1

How about `,` instead of `;`?

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

```

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [January 3, 2019, 9:54pm UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/4 "2019-01-03T21:54:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)\
**Post date:** [January 4, 2019, 12:57am UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/5 "2019-01-04T00:57:47Z")

</div>

How about parsing

```julia
map(args) do x, y=1
   #body
end

```

as the function with default values for positional arguments:

```julia
map(function (x, y=1)
    #body 
end, args)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 4, 2019, 8:50am UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/6 "2019-01-04T08:50:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 4, 2019, 9:27am UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/7 "2019-01-04T09:27:26Z")

</div>

I asked following the question in [Can I pass keyword args to do-blocks in julia? - Stack Overflow](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):

```julia
map(round, x, digits=3)

```

as an equivalent of

```julia
round.(x, digits=3)

```

(the general pattern could be the case when the caller function passes through its keyword arguments to a called function)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 4, 2019, 9:31am UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/8 "2019-01-04T09:31:31Z")

</div>

Thanks. I understand you are not advocating this, but I think that

> [@bkamins](#):
>
> ```julia
> map(round, x, digits=3)
> 
> ```
> 
> as an equivalent of
> 
> ```julia
> round.(x, digits=3)
> 
> ```

would be very confusing, as arguments would end up mapped or not based on being keyword or positional.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [January 4, 2019, 10:08am UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/9 "2019-01-04T10:08:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [October 24, 2022, 5:16pm UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/10 "2022-10-24T17:16:48Z")

</div>

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](https://docs.julialang.org/en/v1/manual/functions/#:~:text=With%20the%20do%20block%20syntax,variables%20from%20its%20enclosing%20scope).

---

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [August 13, 2024, 1:51am UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/11 "2024-08-13T01:51:49Z")

</div>

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](https://github.com/c42f) ?

SEE ALSO

- [do-block with keyword arguments · Issue #50395 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/50395)
- [Do-block syntax with keyword arguments](https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111/)

---

<div class="post-metadata">

**Author:** ![cheengshuchin](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@cheengshuchin](https://discourse.julialang.org/u/cheengshuchin)\
**Post date:** [October 22, 2024, 10:38am UTC](https://discourse.julialang.org/t/allowing-for-keyword-arguments-in-do-block/19244/12 "2024-10-22T10:38:39Z")

</div>

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:

> <https://github.com/JuliaLang/julia/issues/50395#issuecomment-2425060604>
>
> 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.

> <https://github.com/JuliaLang/julia/issues/50395#issuecomment-2425163220>
>
> 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.
