UndefVarError: Using `TypeVar` both in argument type and return type

I have three methods

julia> f(::T) where {T <: Int} = 2
f (generic function with 1 method)

julia> f(::T)::T where {T <: Float64} = 2.0
ERROR: UndefVarError: T not defined
 [1] top-level scope at none:0

julia> f(::String)::T where {T <: String} = ""
f (generic function with 2 methods)

Why is the 2nd example not working? How can I fix it?

Julia version: 1.1.0

 (f(::T)::T) where {T <: Float64} = 2.0


although I’m not sure how helpful that is
f(::Float64)::Float64 = 2.0 does the same thing
as does f(::Float64) = 2.0

to go deeper see Eric Davies’ presentation on Return Types

1 Like

Thank you, I forgot this!