danvenn
February 22, 2021, 6:52pm
1
Hi, I’m really liking Julia so far, but the documentation on the usage of where has me confused (despite finding a few posts here discussing it).
What would be the difference between:
same_type(x::T, y::T) where {T} = true
and
same_type(x::T, y::T) where T = true?
Thanks
pixel27
February 22, 2021, 7:21pm
2
There is no difference. You need the {} when you have multiple types i.e:
same_type(x::S, y::T) where {S, T} = true
Without the {} you get a parsing error:
julia> same_type(x::S, y::T) where S, T = true
ERROR: UndefVarError: T not defined
Stacktrace:
[1] top-level scope at none:1
So really where T
is just a convenience to save some keystrokes if there is only 1 type.
5 Likes
mbauman
February 22, 2021, 7:46pm
4
The curlies also make the single-clause version easier for my brain to parse. So it’s optional, but I find it useful for not accidentally misreading the where T = ...
as having the function body defining what T
is!
10 Likes