Ch 17 - Multiple Diospatch Flashcards
Think Julia
What is a: generic programming
In Julia you have the ability to write code [functions] that can operate on different types. This is called “generic programming.”
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6279-6280). Kindle Edition.
Think Julia
What is a: Type Declarations
The :: operator attaches type annotations to expressions and variables, indicating what types they should have:
julia > (1 + 2) :: Float64
ERROR: TypeError: in typeassert, expected Float64, got Int64
julia > (1 + 2) :: Int64
3
This helps to confirm that your program works the way you expect.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6283-6295). Kindle Edition.
Think Julia
What is a: ‘ :: ‘
The :: operator attaches type annotations to expressions and variables, indicating what types they should have:
julia > (1 + 2) :: Float64
ERROR: TypeError: in typeassert, expected Float64, got Int64
julia > (1 + 2) :: Int64
3
This helps to confirm that your program works the way you expect.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6283-6295). Kindle Edition.
Think Julia
On which side of an assignment should ‘ :: ‘ be placed?
The :: operator attaches type annotations to expressions and variables, indicating what types they should have:
julia > (1 + 2) :: Float64
ERROR: TypeError: in typeassert, expected Float64, got Int64
julia > (1 + 2) :: Int64
3
This helps to confirm that your program works the way you expect.
The :: operator can also be appended to the lefthand side of an assignment, or included as part of a declaration:
julia > function returnfloat()
….x::Float64 = 100
….x
end
returnfloat (generic function with 1 method)
julia > x = returnfloat()
100.0 julia > typeof( x) Float64
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6304-6305). Kindle Edition.
Think Julia
How can a type annotation be attached to a function’s definition?
A type annotation can also be attached to the header of a function definition:
function sinc( x):: Float64
….if x = = 0
……..return 1
….end
….sin( x)/( x)
end
The return value of sinc is always converted to type Float64.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6307-6315). Kindle Edition.
Think Julia
Should type declarations be used on the fields of structs.
using Printf
struct MyTime
….hour :: Int64
….minute :: Int64
….second :: Int64
end
function printtime( time)
….@printf(“% 02d:% 02d:% 02d”, time.hour, time.minute, time.second)
end
As you can see, type declarations can (and should, for performance reasons) be added to the fields in a struct definition.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Location 6330). Kindle Edition.
Think Julia
How can a function definition assure that only a specific stuct object is passed as an argument?
To add a method to the function printtime that only accepts a MyTime object as an argument, all we have to do is append :: followed by MyTime to the argument time in the function definition:
function printtime( time:: MyTime)
….@printf(“% 02d:% 02d:% 02d”, time.hour, time.minute, time.second)
end
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6337-6351). Kindle Edition.
Think Julia
What is a: method
function printtime( time:: MyTime)
….@printf(“% 02d:% 02d:% 02d”, time.hour, time.minute, time.second)
end
A method is a function definition with a specific signature: printtime has one argument of type MyTime.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6351-6353). Kindle Edition.
Think Julia
What is a: constructor
A constructor is a special function that is called to create an object. The default constructor methods of MyTime, which take all fields as parameters, have the following signatures:
MyTime( hour, minute, second)
MyTime( hour:: Int64, minute:: Int64, second:: Int64)
We can also add our own outer constructor methods:
function MyTime( time:: MyTime)
….MyTime( time.hour, time.minute, time.second)
end
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6421-6437). Kindle Edition.
Think Julia
What is a: outer constructor
We can also add our own outer constructor methods:
function MyTime( time:: MyTime)
….MyTime( time.hour, time.minute, time.second)
end
This method is called a copy constructor because the new MyTime object is a copy of its argument.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6438-6440). Kindle Edition.
Think Julia
What is a: inner constructor
To enforce invariants, we need inner constructor methods:
struct MyTime
….hour :: Int64
….minute :: Int64
….second :: Int64
….function MyTime( hour:: Int64 = 0, minute:: Int64 = 0, second:: Int64 = 0)
……..@assert( 0 ≤ minute < 60, “Minute is not between 0 and 60.”)
……..@assert( 0 ≤ second < 60, “Second is not between 0 and 60.”) new( hour, minute, second)
….end
end
The struct MyTime now has four inner constructor methods:
MyTime()
MyTime( hour:: Int64)
MyTime( hour:: Int64, minute:: Int64)
MyTime( hour:: Int64, minute:: Int64, second:: Int64)
An inner constructor method is always defined inside the block of a type declaration, and it has access to a special function called new that creates objects of the newly declared type.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6461-6469). Kindle Edition.
Think Julia
What is a: show()
show
show is a special function that returns a string representation of an object. For example, here is a show method for MyTime objects:
using Printf
function Base.show( io:: IO, time:: MyTime)
@printf( io, “% 02d:% 02d:% 02d”, time.hour, time.minute, time.second)
end
The prefix Base is needed because we want to add a new method to the Base.show function.
When you print an object, Julia invokes the show function:
julia > time = MyTime( 9, 45)
09: 45: 00
When I write a new composite type, I almost always start by writing an outer constructor, which makes it easier to instantiate objects, and a show method, which is useful for debugging.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6496-6516). Kindle Edition.
Think Julia
What does operator overloading look like for the + symbol?
MyTime arguments, you can use the + operator on MyTime objects. Here is what the definition might look like:
import Base. +
function +( t1:: MyTime, t2:: MyTime)
….seconds = timetoint( t1) + timetoint( t2)
….inttotime( seconds)
end
The import statement adds the + operator to the local scope so that methods can be added.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6521-6532). Kindle Edition.
Think Julia
What is a: multiple dispatch
The dispatch mechanism determines which method to execute when a function is called. Julia allows the dispatch process to choose which of a function’s methods to call based on the number of arguments given, and on the types of all of the function’s arguments. Using all of a function’s arguments to choose which method should be invoked is known as multiple dispatch.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6566-6569). Kindle Edition.
Think Julia
What is a: generic programming
Generic Programming
Multiple dispatch is useful when it is necessary, but (fortunately) it is not always necessary. Often you can avoid it by writing functions that work correctly for arguments with different types. This is known as generic programming.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6576-6579). Kindle Edition.