-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiple-Dispatch.jl
125 lines (79 loc) · 3.6 KB
/
Multiple-Dispatch.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Multiple Dispatch
#=
In this notebook we'll explore *multiple dispatch*, which is a key feature of Julia.
Multiple dispatch makes software generic and fast!
=#
# Starting with the familiar
#=
To understand multiple dispatch in Julia, let's start with what we've already seen.
We can declare functions in Julia without giving Julia any information about the
types of the input arguments that function will receive:
=#
f(x) = x.^2
# and then Julia will determine on its own which input argument types make sense and which do not:
f(10)
f([1, 2, 3])
# Specifying the types of our input arguments
#=
However, we also have the option to tell Julia explicitly what types our input arguments are allowed to have.
For example, let's write a function foo that only takes strings as inputs.
=#
foo(x::String, y::String) = println("My inputs y are both strings!")
#=
We see here that in order to restrict the type of x and y to Strings,
we just follow the input argument name by a double colon and the keyword String.
Now we'll see that foo works on Strings and doesn't work on other input argument types.
=#
foo("hello", "hi!")
foo(3, 4)
#=
To get foo to work on integer (Int) inputs, let's tack ::Int onto our input
arguments when we declare foo.
=#
foo(x::Int, y::Int) = println("My inputs y are both integers!")
foo(3, 4)
# Now foo works on integers! But look, foo also still works when x and y are strings!
foo("hello", "hi!")
# This is starting to get to the heart of multiple dispatch. When we declared
foo(x::Int, y::Int) = println("My inputs y are both integers!")
# we didn't overwrite or replace
foo(x::String, y::String)
#=
Instead, we just added an additional *method* to the *generic function* called foo.
A *generic function* is the abstract concept associated with a particular operation.
For example, the *generic function* + represents the concept of addition.
A *method* is a specific implementation of a *generic function* for particular argument types.
For example, + has methods that accept floating point numbers, integers, matrices, etc.
We can use the methods to see how many methods there are for foo.
=#
methods(foo)
# Aside: how many methods do you think there are for addition?
methods(+)
#=
So, we now can call foo on integers or strings. When you call foo on a particular
set of arguments, Julia will infer the types of the inputs and dispatch
the appropriate method. This is multiple dispatch.
Multiple dispatch makes our code generic and fast. Our code can be generic and
flexible because we can write code in terms of abstract operations such as addition
and multiplication, rather than in terms of specific implementations.
At the same time, our code runs quickly because Julia is able to call efficient
methods for the relevant types.
=#
# To see which method is being dispatched when we call a generic function, we can use the @which macro:
@which foo(3, 4)
# Let's see what happens when we use @which with the addition operator!
@which 3.0 + 3.0
#=
And we can continue to add other methods to our generic function foo.
Let's add one that takes the *abstract type* Number, which includes subtypes
such as Int, Float64, and other objects we would think of as numbers:
=#
foo(x::Number, y::Number) = println("My inputs y are both numbers!")
# This method for foo will work on, for example, floating point numbers:
foo(3.0, 4.0)
# We can also add a fallback, duck-typed method for foo that takes inputs of any type:
foo(x, y) = println("I accept inputs of any type!")
# Given the methods we've already written for foo so far,
# this method will be called whenever we pass non-numbers to foo:
v = rand(3)
foo(v, v)