Created
April 24, 2014 16:57
-
-
Save alco/11261697 to your computer and use it in GitHub Desktop.
This file contains 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 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