# Do-block syntax with keyword arguments

**URL:** <https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111>\
**Category:** General Usage\
**Tags:** question, syntax\
**Created:** [July 3, 2023, 8:42am UTC](https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111 "2023-07-03T08:42:35Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [July 3, 2023, 8:42am UTC](https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111/1 "2023-07-03T08:42:35Z")

</div>

Is is possible to use the `do` syntax with keyword arguments? MWE:

```julia
just_call(f, args...; kwargs...) = f(args...; kwargs...)

# lambda
just_call((x; z) -> @show(x, z), 1; z = 2)

# do
just_call(1; z = 2) do x; z
    @show x, z
end

```

but the latter fails with

```julia
ERROR: MethodError: no method matching (::var"#20#21")(::Int64; z::Int64)

Closest candidates are:
  (::var"#20#21")(::Any) got unsupported keyword argument "z"
   @ Main REPL[30]:1

```

The [manual](https://docs.julialang.org/en/v1/manual/functions/#Do-Block-Syntax-for-Function-Arguments) does not talk about this, and I could not find an open issue.

---

<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:** [July 3, 2023, 12:11pm UTC](https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111/2 "2023-07-03T12:11:19Z")

</div>

Opened an issue

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

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 3, 2023, 12:31pm UTC](https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111/3 "2023-07-03T12:31:25Z")

</div>

Surprising that this has not crop up before, but now I think that most methods in `Base` (if not all) that have the first parameter as a function do not pass anything as keyword argument to them.

---

<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:** [February 10, 2024, 7:56am UTC](https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111/4 "2024-02-10T07:56:28Z")

</div>

That can be tweaked functionally with closures

```julia
# let's define
_print_vec2d((x,y)) = println("vec2d($x,$y)")

# and repl these
_print_vec2d((1,2))
foreach(_print_vec2d, [(1,2), (3,4), (5,6)])
foreach([(1,2), (3,4), (5,6)]) do xy; _print_vec2d(xy) end

```

now one can augment it with kw args

```julia
import LinearAlgebra: norm2
withkws(f; kwargs...) = (args...) -> f(args...; kwargs...)

_printx_vec2d((x,y); dounit::Bool) = begin
    if dounit
        n2 = norm2((x,y))
        rnd3(v) = round(v, digits=3)
        (x,y) = (rnd3(x/n2), rnd3(y/n2))
    end
    println("vec2d($x,$y)")
end

# and repl these
_printx_vec2d((1,2); dounit=true)
foreach(withkws(_printx_vec2d; dounit=true), [(1,2), (3,4), (5,6)])
foreach([(1,2), (3,4), (5,6)]) do xy
    withkws(_printx_vec2d; dounit=true)(xy)
    # or
    # xy |> withkws(_printx_vec2d; dounit=true)
end

```

---

<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:** [February 10, 2024, 1:37pm UTC](https://discourse.julialang.org/t/do-block-syntax-with-keyword-arguments/101111/5 "2024-02-10T13:37:26Z")

</div>

NB: impl of this at the syntaxic level may imply a special do form like this

```julia
foreach([(1,2), (3,4), (5,6)]) do xy; withkws(; dounit=true) do (xy; dounit)
    _printx_vec2d(xy; dounit)
end end

```

otherwise ‘;’ will be catched as a newline / instruction separator
