The Julia documentation seems to be conflicting about when to use type annotations.
From the Types documentation:
The
::operator can be used to attach type annotations to expressions and variables in programs. There are two primary reasons to do this:
- [...]
- To provide extra type information to the compiler, which can then improve performance in some cases.
From Performance Tips:
Type annotation will not enhance (and can actually hinder) performance if the type is abstract, or constructed at run-time.
For the purposes of illustration, suppose I had the following hierarchy:
abstract type Character end
struct SpaceMarine <: Character end
# I've used the abstract type Character as a type annotation.
svetlana :: Character = SpaceMarine()
Is using type annotations the way I did (note that it's abstract), hindering performance or helping it?
In this case, svetlana can only be a subtype of Character, which is the behaviour that I require.
Just as in C# or Java, you can do Character svetlana = new SpaceMarine(), svetlana can only be any subtype of Character.
If using type annotations is hurting performance, how can I keep svetlana a type of Character?
I don't want someone to write svetlana = "Space Marine". In this case, it's now a String.