Deprecated syntax parametric method syntax with type of output

Hello,

I’m fixing some Julia 0.6 code to Julia 0.7/1.0

short{T<:Real}(qty::T)::T = qty

I was trying

julia> short(qty::T)::T where {T<:Real} = qty
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] top-level scope at none:0

I fixed it using

short(qty::T) where {T<:Real} = qty

I wonder why I can’t define T as a parametric type of output like this.

Any idea?

This works (as long as you don’t need T in the function body):

julia> short(qty::T where T<:Real)::T = qty                                                                                                                             
short (generic function with 1 method)                                                                                                                                 

Edit: scrap that:

julia> short(5)                                                                                                                                                         
ERROR: UndefVarError: T not defined                                                                                                                                     
Stacktrace:                                                                                                                                                             
 [1] short(::Int64) at ./REPL[1]:1                                                                                                                                      
 [2] top-level scope at none:0                                                                                                                                          

because the return type declaration is “in the body”.

You need to be a bit more explicit in this case:

julia> (short(qty::T)::T) where {T<:Real} = qty
short (generic function with 1 method)

julia> short(3)
3

Your definition is ambigous and could be parsed as short(qty::T)::(T where {T<:Real}) = qty, which would leave the first T undefined.

3 Likes

Thanks @pfitzseb for your help