Skip to content

Instantly share code, notes, and snippets.

@alco
Created April 24, 2014 16:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alco/11261697 to your computer and use it in GitHub Desktop.
Save alco/11261697 to your computer and use it in GitHub Desktop.
defmodule M do
defmacro common_fields(extra \\ []) do
[color: nil, id: nil] ++ extra
end
end
defmodule Pawn do
import M
defstruct common_fields
end
defmodule King do
import M
defstruct common_fields([moved: false])
end
defprotocol Move do
def valid_moves(piece)
end
defimpl Move, for: Pawn do
def valid_moves(_) do
IO.puts "I am a pawn"
end
end
defimpl Move, for: King do
def valid_moves(_) do
IO.puts "I am a king"
end
end
defmodule T do
def test do
Move.valid_moves(%Pawn{})
Move.valid_moves(%King{})
end
end
T.test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment