I’ve been having trouble with this, and I finally found out why.

It has to do with the precedence of `::`

and `where`

in function signatures.

Here’s an example of where this goes wrong:

```
firstint(a::Vector{T})::Int where {T<:Integer} = first(a)
```

This will give you an error about `T`

being undefined. This is because you are specifying the return type as `Int where {T<:Integer}`

, then trying to use this type variable outside of its scope.

The fix is to parenthesize the function signature:

```
(firstint(a::Vector{T})::Int) where {T<:Integer} = first(a)
```

This is unintuitive, as function definitions cannot be parenthesized like this.

It also seems quite obscure, as I couldn’t find anything about it online, and only figured it out by using `dump`

to reverse engineer the needed `Expr`

, then seeing how it was printed. If there is a better way to write this, let me know.

This isn’t some minor problem either. I have had this come up in most of the projects I’ve been working on recently.

One thing that would help is giving a warning for unused type variables, and perhaps showing this syntax if the unused type variable occurs in the return type of a function.

The other idea is when an `UndefVarError`

occurs, look for type variables declared with a lesser scope, and suggest moving them out.

This would also help in situations like this:

```
element_type(a::Vector{T} where{T}) = T
```