Better introduction to parametric types in the manual
#43,811 opened on Jan 14, 2022
Description
After a discussion in IRC today I realised that there were a few things about parametric types that aren't easily understood. It seems that the manual could help a bit more in introducing parametric types. I'm opening this issue to outline some of the things I think could be documented better, and also to gather ideas or examples that might help to write a more hands-on parametric types section in the manual. Not sure if this belongs on the forum but I am thinking of linking a PR at some point.
For the parametric type Foo and an instance foo, I think the docs could clarify
- why both
isabstracttype(Foo)andisconcretetype(Foo)return false for the parametric typeFoo, i.e. that parametric types are neither abstract nor concrete - the difference between the following definitions of
Foo
struct Foo{T}
a::T
b::T
end
struct Foo{T1, T2}
a::T1
b::T2
end
- why
typeof(foo) <: Foois true, howeversupertype(typeof(foo)) == Foois false (it'sAny) - why
Foo isa DataTypeis false
and (more visibly) inform
that parametric types cannot be subtypedthey can, parametric abstract types are a thing- that reified instances of parametric types are of a concrete type, therefore
typeof(foo) != Foowherefoois an instance ofFoo - that
foo isa Foomust be used to test iffoois an instance ofFoo
I also think there should be one example on how to set up a simple but complete type hierarchy (without going into all of the gritty details of the Advanced Types section). Something like the comment by wasshin in #4935.
There might be some other things I've forgotten, feel free to suggest related improvements. I feel that parametric types are one of the major features of Julia's type system, however they can be confusing when coming from languages with more traditional/simple OOP systems. Understanding parametric types should help to reduce common antipatterns like over-constraining argument types, and help people to leverage the dispatch system (what are all those <: needed for anyway?)