Skip to content

Instantly share code, notes, and snippets.

@Adzz
Created May 27, 2018 12:15
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/e97d0be53ebe5a75f97129d689bf2c8f to your computer and use it in GitHub Desktop.
Save Adzz/e97d0be53ebe5a75f97129d689bf2c8f to your computer and use it in GitHub Desktop.
defmodule Square do
defstruct [:side]
end
defmodule Circle do
defstruct [:radius]
end
defprotocol Area do
def calculate_for(shape)
end
defprotocol Perimeter do
def calculate_for(shape)
end
defimpl Area, for: Square do
def calculate_for(square) do
square.side * square.side
end
end
defimpl Perimeter, for: Circle do
def calculate_for(circle) do
circle.radius * 2 * 3.14
end
end
Area.calculate_for(%Square{side: 10})
Perimeter.calculate_for(%Circle{radius: 10})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment