This chapter covers
- Understanding type hierarchies
- Difference between abstract and concrete types
- Combining primitive types to make composite types
- Harness the power of multiple-dispatch to solve complex tasks elegantly
- How multiple-dispatch differs from single-dispatch in object-orientedlanguages[21]
All objects in Julia are of a particular type. Remember you can use typeof
to discover the type of any object:
julia> typeof(42) Int64 julia> typeof('A') Char julia> typeof("hello") String
The type decides what you can do with an object. For example, a dictionary allows you to look up a value by key, while an array stores elements in order. An expression evaluating to a Bool
value such as true
or false
can be used in if-statement and while-loops while expressions evaluating to a floating-point value can’t:
julia> if 2.5 print("this should not be possible") end ERROR: TypeError: non-boolean (Float64) used in boolean context
Thus, if you want to create objects with different behavior and features, you need to define new types. In programming, we often try to mimic the real world: