#MonthOfJulia Day 6: Composite Types
Composite types are declared with the
type keyword. To illustrate we’ll declare a type for storing geographic locations, with attributes for latitude, longitude and altitude. The type immediately has two methods: a default constructor and a constructor specialised for arguments with data types corresponding to those of the type’s attributes. More information on constructors can be found in the documentation.
Creating instances of this new type is simply a matter of calling the constructor. The second instance below clones the type of the first instance. I don’t believe I’ve seen that being done with another language. (That’s not to say that it’s not possible elsewhere! I just haven’t seen it.)
We can list, access and modify instance attributes.
Additional “outer” constructors can provide alternative ways to instantiate the type.
Of course, we can have collections of composite types. In fact, these composite types have essentially all of the rights and privileges of the built in types.
GeographicLocation type declared above is a “concrete” type because it has attributes and can be instantiated. You cannot derive subtypes from a concrete type. You can, however, declare an abstract type which acts as a place holder in the type hierarchy. As opposed to concrete types, an abstract type cannot be instantiated but it can have subtypes.
immutable keyword will create a type where the attributes cannot be modified after instantiation.
Additional ramblings and examples of composite types can be found on github. Also I’ve just received an advance copy of Julia in Action by Chris von Csefalvay which I’ll be reviewing over the next week or so.