Skip to content

Instantly share code, notes, and snippets.

@angelikatyborska
Created August 15, 2021 09:35
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 angelikatyborska/15e00919e0e8c846732b619e5dab6f65 to your computer and use it in GitHub Desktop.
Save angelikatyborska/15e00919e0e8c846732b619e5dab6f65 to your computer and use it in GitHub Desktop.
Protocol consolidation vs mix test --no-compile
# test/rpg_test.exs
defmodule RPGTest do
use ExUnit.Case
alias RPG.{Edible, LoafOfBread}
describe "LoafOfBread" do
test "implements the Edible protocol" do
{:consolidated, modules} = Edible.__protocol__(:impls)
assert LoafOfBread in modules
end
end
end
# lib/rpg.ex
defmodule RPG do
defmodule Character do
defstruct health: 100, mana: 0
end
defmodule LoafOfBread do
defstruct []
end
defprotocol Edible do
def eat(item, character)
end
defimpl Edible, for: LoafOfBread do
def eat(_loaf_of_bread, %Character{} = character) do
updated_character = %{character | health: character.health + 5}
{nil, updated_character}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment