Skip to content

Instantly share code, notes, and snippets.

@Adzz
Last active November 28, 2018 16:05
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/99253ac7ce382b62b028ae46efe67fd0 to your computer and use it in GitHub Desktop.
Save Adzz/99253ac7ce382b62b028ae46efe67fd0 to your computer and use it in GitHub Desktop.
defmodule AmazeOn do
defstruct [price: 10]
end
defmodule Area do
defstruct []
end
defmodule Square do
defstruct [:side]
end
defprotocol ShapePricePerShop do
def calculate(calculation, shape, shop)
end
defprotocol AreaProtocol do
def calculate(shape, shop)
end
defprotocol SquareProtocol do
def calculate(shop, shape)
end
defimpl SquareProtocol, for: AmazeOn do
def calculate(%AmazeOn{price: price}, %Square{side: side}) do
side * side * price
end
end
defimpl AreaProtocol, for: Square do
def calculate(shape = %Square{}, shop) do
SquareProtocol.calculate(shop, shape)
end
end
defimpl ShapePricePerShop, for: Area do
def calculate(%Area{}, shape, shop) do
AreaProtocol.calculate(shape, shop)
end
end
ShapePricePerShop.calculate(%Area{}, %Square{side: 10}, %AmazeOn{})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment