{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multiple dispatch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook we'll explore **multiple dispatch**, which is a key feature of Julia.\n", "\n", "Multiple dispatch makes software *generic* and *fast*!\n", "\n", "#### Starting with the familiar\n", "\n", "To understand multiple dispatch in Julia, let's start with what we've already seen.\n", "\n", "We can declare functions in Julia without giving Julia any information about the types of the input arguments that function will receive:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f (generic function with 1 method)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(x) = x^2" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "ename": "ArgumentError", "evalue": "ArgumentError: Package InteractiveShell not found in current path:\n- Run `import Pkg; Pkg.add(\"InteractiveShell\")` to install the InteractiveShell package.\n", "output_type": "error", "traceback": [ "ArgumentError: Package InteractiveShell not found in current path:\n- Run `import Pkg; Pkg.add(\"InteractiveShell\")` to install the InteractiveShell package.\n", "", "Stacktrace:", " [1] require(::Module, ::Symbol) at .\\loading.jl:892", " [2] top-level scope at In[38]:1" ] } ], "source": [ "import InteractiveShell\n", "InteractiveShell.ast_node_interactivity = \"all\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and then Julia will determine on its own which input argument types make sense and which do not:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(10)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "ename": "MethodError", "evalue": "MethodError: no method matching ^(::Array{Int64,1}, ::Int64)\nClosest candidates are:\n ^(!Matched::Float16, ::Integer) at math.jl:885\n ^(!Matched::Regex, ::Integer) at regex.jl:712\n ^(!Matched::Missing, ::Integer) at missing.jl:155\n ...", "output_type": "error", "traceback": [ "MethodError: no method matching ^(::Array{Int64,1}, ::Int64)\nClosest candidates are:\n ^(!Matched::Float16, ::Integer) at math.jl:885\n ^(!Matched::Regex, ::Integer) at regex.jl:712\n ^(!Matched::Missing, ::Integer) at missing.jl:155\n ...", "", "Stacktrace:", " [1] macro expansion at .\\none:0 [inlined]", " [2] literal_pow at .\\none:0 [inlined]", " [3] f(::Array{Int64,1}) at .\\In[1]:1", " [4] top-level scope at In[3]:1" ] } ], "source": [ "f([1, 2, 3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Specifying the types of our input arguments\n", "\n", "However, we also have the *option* to tell Julia explicitly what types our input arguments are allowed to have.\n", "\n", "For example, let's write a function `foo` that only takes strings as inputs." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "foo (generic function with 4 methods)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo(x::String, y::String) = println(\"My inputs x and y are both strings!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see here that in order to restrict the type of `x` and `y` to `String`s, we just follow the input argument name by a double colon and the keyword `String`.\n", "\n", "Now we'll see that `foo` works on `String`s and doesn't work on other input argument types." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My inputs x and y are both strings!\n" ] } ], "source": [ "foo(\"hello\", \"hi!\")" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My inputs x and y are both integers!\n", "foo(3, 4) = nothing\n" ] }, { "data": { "text/plain": [ "foo (generic function with 4 methods)" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@show foo(3, 4)\n", "foo(x, y) = x^y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get `foo` to work on integer (`Int`) inputs, let's tack `::Int` onto our input arguments when we declare `foo`." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "foo (generic function with 3 methods)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo(x::Int, y::Int) = println(\"My inputs x and y are both integers!\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My inputs x and y are both integers!\n" ] } ], "source": [ "foo(3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now `foo` works on integers! But look, `foo` also still works when `x` and `y` are strings!" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My inputs x and y are both strings!\n" ] } ], "source": [ "foo(\"hello\", \"hi!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is starting to get to the heart of multiple dispatch. When we declared\n", "\n", "```julia\n", "foo(x::Int, y::Int) = println(\"My inputs x and y are both integers!\")\n", "```\n", "we didn't overwrite or replace\n", "```julia\n", "foo(y::String, y::String)\n", "```\n", "Instead, we just added an additional ***method*** to the ***generic function*** called `foo`.\n", "\n", "A ***generic function*** is the abstract concept associated with a particular operation.\n", "\n", "For example, the generic function `+` represents the concept of addition.\n", "\n", "A ***method*** is a specific implementation of a generic function for *particular argument types*.\n", "\n", "For example, `+` has methods that accept floating point numbers, integers, matrices, etc.\n", "\n", "We can use the `methods` to see how many methods there are for `foo`." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "# 3 methods for generic function foo: