7 Understanding types

 

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:

7.1 Creating Composite Types From Primitive Types

 
 

7.2 Exploring type hierarchies

 
 

7.3 Creating a Battle Simulator

 
 
 

7.3.1 Defining warrior types

 
 

7.3.2 Adding behavior to warriors

 
 

7.3.3 Using Multiple Dispatch to Invoke Methods

 
 

7.4 How Julia selects method to call

 
 

7.4.1 ASTs for the Curious

 
 

7.4.2 Contrasting Julia’s Multiple-Dispatch With Object-Oriented Languages

 

7.4.3 How is Multiple-Dispatch Different from Function Overloading?

 
 
 

7.5 Summary

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest