Last active
May 29, 2018 22:04
-
-
Save Adzz/4d6d9fc6e97ed2a6bf9e95c74cb4d8dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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