Differences between abstract and concrete types

Having discussed abstract types and concrete types, you may be wondering how they differ from each other. We can summarize their differences in the following table:

Property Abstract Type Concrete Type
Has a supertype? Yes Yes
Allows subtypes? Yes No
Contains data fields? No Yes
First class? Yes Yes
Can be part of a Union type? Yes

Yes

 

For abstract types, we can build a hierarchy of types. The top-level type is just Any. Abstract types cannot contain any data fields as they are used to represent concepts rather than data storage. Abstract types are first-class, meaning that they can be stored and passed around, and there are functions that work with them—for example, the isa and <: operators. 

A concrete type is associated with an abstract type as a supertype. If the supertype is not specified, then it is assumed to be Any. A concrete type does not allow subtypes. This means that every concrete type must be final, and would be a leaf node in a type hierarchy. Concrete types are also first class, just like abstract types.

Both abstract types and concrete types can be referred to in a Union type.

What we just mentioned may be surprising to people who come from an object-oriented programming background. First, you may wonder why concrete types do not allow subtypes. Second, you may wonder why abstract types can't be defined with fields. This design is actually intentional and was debated heavily by the core Julia development team. The debate is related to behavioral inheritance versus structural inheritance, which will be discussed in Chapter 12, Inheritance and Variance.

Now, let's switch gear and go over the parametric type feature of the Julia language.

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

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