Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Polymorphism

polymorphism.livemd

Polymorphism

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

Polymorphism

The word polymorphism comes from the Greek word for “many shapes”. It means essentially the same thing for our programs. We often want to define a single general behavior, but apply it to different things.

  flowchart
    A[Polymorphic Behavior] --- B[Specific Implementation]
    A --- C[Specific Implementation]
    A --- D[Specific Implementation]
    A --- E[Specific Implementation]
    A --- F[Specific Implementation]

Running is a great real-world example of polymorphism. Many different animals run, and they all run in different ways.

  flowchart
    A[Run] --- B[Cheetahs]
    A --- C[Cats]
    A --- D[Dogs]
    A --- E[Horses]
    A --- F[Humans]

For a more technical example, you can think of many devices that need to implement an off feature. You can turn off your computer, your tv, your lights, and your phone. Each needs to do this differently under the hood, but the behavior is universal.

  flowchart
    A[off] --- B[TV]
    A --- C[Computer]
    A --- D[Phone]
    A --- E[Lights]

Anytime you have a universal behavior, but separate implementation depending on some context, it’s an opportunity to use polymorphism to improve the clarity of your code.

You’ve also been using the Enum module which provides polymorphic behavior for different collection data types.

  flowchart
    A[Enum] --- B[map]
    A --- C[list]
    A --- D[keyword-list]
    A --- E[range]

You have also already learned some methods of achieving polymorphism with multi-clause functions.

defmodule Greeter do
  def hi(name) do
    "Hi #{name}!"
  end

  def hi(name1, name2) do
    "Hi #{name1} and #{name2}!"
  end

  def hi(_name1, _name2, _name3) do
    "Hi everyone!"
  end
end

For example, multiple arity functions could be said to be polymorphic, They create the same generic behavior but with different implementations depending on the number of arguments given.

  flowchart
    A[Greeter.hi] --> B[1 person]
    A --> C[2 people]
    A --> D[3 people]

Polymorphism can be achieved through many means. You will learn more about the tools available in Elixir to produce behavior as you go through the course.

Your Turn

Think of an example of polymorphic behavior that you encounter in every day life. What is some common abstraction you often use which actually requires different specific behavior?

Running is one example, can you think of others?

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish polymorphism section"