Proper way to declare return types parametrically in short-form functions in Julia 0.6

In Julia 0.5, I was able to declare the return type with a type parameter as follows:

julia> test{T<:Number}(x::T)::float(T) = x
test (generic function with 1 method)

julia> test(1)
1.0

However, in Julia 0.6 with the where {T} syntax, the following similar definition doesn’t work:

julia> test(x::T)::float(T) where {T} = x
ERROR: UndefVarError: T not defined

What is the proper way to do this in 0.6? I’ve found that the following works:

julia> (test(x::T)::float(T)) where {T<:Number} = x
test (generic function with 1 method)

julia> test(1)
1.0

but is this a standard way of doing this? Enclosing the method signature with a pair of parentheses looks a bit ugly…

As a side note, I’m aware that the long-form definition works fine:

julia> function test(x::T)::float(T) where {T<:Number}
           x
       end
test (generic function with 1 method)

julia> test(1)
1.0

As far as I’m aware, the extra parentheses are the way to go. See also Return type assertions with type parameters - #2 by rdeits

2 Likes