Suggestion: Allow type variables with `do` syntax

It came up in Do-Block Syntax with Where Keyword - #16 by sgaure whether it was possible to use type variables in anonymous functions created with the do syntax. Although I do not think it would solve the particular problem there, and otherwise will be of limited utility, it would be nice to have just to be complete.

I looked into it. It turns out it can be enabled with a one-line change in the parse_do function in JuliaSyntax/src/parser.jl:

<     emit(ps, m, K"tuple")
>     peek_behind(ps).kind != K"where" && emit(ps, m, K"tuple")

It will require the do-argument list to be enclosed in parentheses:

foreach([1, 2.0, "foo", false]) do (x::T) where T

Since a where clause in a do argument list currently leads to a syntax error, I doubt this will break anything. What do people think?

I do not know if introducing new syntax is worth it when retrieving T is just as easy as writing T = typeof(x) in the first line of the function, right?

1 Like

I agree with that. It’s of limited utility, but it is consistent with the other ways of defining functions.

A PkgEval of all registered packages would be nice, after making a PR. Personally I don’t care about do blocks, though.