# Suggestion: Allow type variables with \`do\` syntax

**URL:** https://discourse.julialang.org/t/suggestion-allow-type-variables-with-do-syntax/115276
**Category:** Internals & Design
**Created:** [June 6, 2024, 11:42am UTC](https://discourse.julialang.org/t/suggestion-allow-type-variables-with-do-syntax/115276 "2024-06-06T11:42:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [June 6, 2024, 11:42am UTC](https://discourse.julialang.org/t/suggestion-allow-type-variables-with-do-syntax/115276/1 "2024-06-06T11:42:50Z")

</div>

It came up in [Do-Block Syntax with Where Keyword - #16 by sgaure](https://discourse.julialang.org/t/do-block-syntax-with-where-keyword/115131/16) 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`:

```julia
2308c2308
< 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:

```julia
foreach([1, 2.0, "foo", false]) do (x::T) where T
    println(T)
end
Int64
Float64
String
Bool

```

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?

---

<div class="post-metadata">

### Author: ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)
#### Post date: [June 6, 2024, 11:54am UTC](https://discourse.julialang.org/t/suggestion-allow-type-variables-with-do-syntax/115276/2 "2024-06-06T11:54:04Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [June 6, 2024, 12:21pm UTC](https://discourse.julialang.org/t/suggestion-allow-type-variables-with-do-syntax/115276/3 "2024-06-06T12:21:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 7, 2024, 7:54am UTC](https://discourse.julialang.org/t/suggestion-allow-type-variables-with-do-syntax/115276/4 "2024-06-07T07:54:47Z")

</div>

> [@sgaure](#):
>
> I doubt this will break anything

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