Current differences in ::T vs T where T

question

#1

I was asked what was the current status of the differences between,

magic(obj::Any) = zero(typeof(obj))

and

magic(obj::T) where T = zero(T)

I believe in 0.6 there were some issues being worked out with performance degradation using where T, but I am not aware of the current status. I usually use :: and leave T when it is absolutely necessary.


#2

I don’t recall what the issue was, but they should be equivalent. Also, the ::Any is not necessary.


#3

Semantically they are the same. However AFAIU if obj::Function then there is a heuristic, that ::Any will only specialized if obj is called in the body of magic. The where version will specialize anyway.


#4

You can experiment:

julia> foo(x::Any) = 1
foo (generic function with 1 method)

julia> foo(x::T) where {T} = 2
foo (generic function with 1 method)  # <- overwrites the old definition

julia> foo(x::T) where {T<:Integer} = 2
foo (generic function with 2 methods)  # <- makes a new method

And:

julia> T where T
Any