January 2020
Intermediate to advanced
532 pages
13h 31m
English
The best part of the holy trait pattern is that we can use it with any object, even when its type belongs to a different abstract type hierarchy. Let's explore the case of literature, where we may define its own type hierarchy as follows:
abstract type Literature endstruct Book <: Literature nameend
Now, we can make it obey the LiquidityStyle trait, as follows:
# assign traitLiquidityStyle(::Type{Book}) = IsLiquid()# sample pricing functionmarketprice(b::Book) = 10.0
Now, we can trade books, just like other tradable assets.
Read now
Unlock full access