Skip to content

Instantly share code, notes, and snippets.

@Adzz
Last active May 29, 2018 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adzz/4d6d9fc6e97ed2a6bf9e95c74cb4d8dc to your computer and use it in GitHub Desktop.
Save Adzz/4d6d9fc6e97ed2a6bf9e95c74cb4d8dc to your computer and use it in GitHub Desktop.
defmodule Square do
defstruct [:side]
end
defmodule Area do
defstruct []
end
defprotocol Shape do
def calculate(calculation, shape)
end
defprotocol AreaProtocol do
def calculate(shape)
end
defimpl AreaProtocol, for: Square do
def calculate(%Square{side: side}) do
side * side
end
end
defimpl Shape, for: Area do
def calculate(%Area{}, shape) do
AreaProtocol.calculate(shape)
end
end
Shape.calculate(%Area{}, %Square{side: 2})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment