January 2020
Intermediate to advanced
532 pages
13h 31m
English
The SimpleTraits.jl package (https://github.com/mauro3/SimpleTraits.jl) may be used to make programming traits a little easier.
Let's try to redo the LiquidityStyle example using SimpleTraits. First, define a trait called IsLiquid, as follows:
@traitdef IsLiquid{T}
The syntax may look a little awkward since the T seems to be doing nothing, but it is actually required because the trait is applicable for a specific type T. The next thing is to assign types to this trait:
@traitimpl IsLiquid{Cash}@traitimpl IsLiquid{Investment}
Then, a special syntax with four colons can be used to define functions that take objects exhibiting the trait:
@traitfn marketprice(x::::IsLiquid) = error("Please implement pricing ...Read now
Unlock full access