Do-Block Syntax with Where Keyword

It’s actually possible with a quite simple macro which rewrites the do syntax to -> syntax. I haven’t tested it thoroughly. I suspect it’s just a bit of luck with the parser.

using MacroTools: @capture

macro wheredo(ex)
    @capture(ex, F_(fargs__) do args__; body__; end) || 
        error("Can't find do construct: $ex")
    :($F($(args...) -> $(body...), $(fargs...))) |> esc
end

@wheredo map(1:4, ["a","b","c","d"]) do (x::S, y::T) where {S, T}
    println("S: ", S, ", T: ",T)
    string(x) * string(y)
end
1 Like