Designing composite types

Composite types are defined with the struct keyword. Let's carry on the example from the preceding abstract type section and continue building our personal asset type hierarchy. We will now create a concrete type called Stock as a subtype of Equity. To keep things simple, we will just represent a stock as a trading symbol and the name of the company:

struct Stock <: Equity
symbol::String
name::String
end

We can instantiate a composite type using the standard constructor, which just takes all the fields as an argument:

Now, since Stock is a subtype of Equity, which is a subtype of Investment, which in turn is a subtype of Asset, we should obey the contract that we set forth earlier by defining the describe function:

function describe(s::Stock)
return s.symbol * "(" * s.name * ")"
end

The describe function just returns a string representation of the stock with both the trading symbol and company name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.106.233