Hi. I understand the basics of types in Julia, but there are some things I don’t quite get, especially with relation to traits and generics and stuff.
For example, I know what this is:
struct Foo{T} where T <: Real end
But I don’t get the difference between these three things:
foo(thing::Vector{T}) where T <: Real = ...
foo(thing::Vector{T} where T <: Real) = ...
foo(thing::Vector{T where T <: Real}) = ...
or these two things:
foo(thing::Vector{T}) where {T} = ...
foo(thing::Vector{T}) where T = ...
but both are in the documentation.
I also don’t really get what can be inside the where clause. Like, you can do some kinds of tests about types, but something like this fails:
foo(thing::T) where IteratorSize(T) == HasLength() = ...
I guess I just don’t understand the semantics of where
very well, except that lets you declare a type variable and make some assertions about it (but I don’t understand what those are.
I see that the way to do traits is like:
foo(iterable) = foo(IteratorSize(iterable), iterable)
foo(::HasLength, iter) = ...
foo(::SizeUnknown, iter) = ...
That seems. Hm… weird. Is there a way to implement a method for any generic iterable? It seems like there must be a way to dispatch based on the availability of certain methods.
Anyway, If anyone has answers to things, that’s good, but I’m really looking for a resource that sets it all out clearly. I can’t seem to find (or perhaps understand?) the answers to these questions from the official docs.