May 2019
Beginner to intermediate
466 pages
10h 44m
English
Sometimes, we might want to allow a function to accept a set of types that are not necessarily part of the same type hierarchy. We could, of course, allow the function to accept any type, but depending on the use case, it could be desirable to strictly limit the arguments to a well-defined subset of types. For such cases, Julia provides type unions.
A type union is a special abstract type that includes as objects all instances of any of its argument types, constructed using the special Union function:
julia> GameEntity = Union{Person,Article}
Union{Article, Person}
Here, we have defined a new type union, GameEntity, which includes two types—Person and Article. Now, we can define functions that know how to handle GameEntities ...
Read now
Unlock full access