Understanding subtyping in Julia
In this recipe, we explain how subtyping in Julia works and how you should design method signatures to properly handle subtyping.
Getting ready
In this recipe, we reveal how to define a type that handles Gaussian integers with additional metadata.
A Gaussian integer is a complex number whose real and imaginary parts are both integers.
Note
In the GitHub repository for this recipe you will find the commands.txt
file that contains the presented sequence of shell and Julia commands.
Now open your favorite terminal to execute commands.
How to do it...
Start with defining the custom Point
type, and next learn how to store it in an array as follows:
- First, define the type that you will be working with:
julia> struct Point{T<:Integer, S<:AbstractString} pos::Complex{T} label::S end julia> Point(x::T, y::T, label::S) where {T<:Integer, S<:AbstractString} = Point{T,S}(Complex...