Skip to content

Instantly share code, notes, and snippets.

@dylan-chong
Created December 17, 2017 18:20
Show Gist options
  • Save dylan-chong/17f92595fd9510abf56f405da8847421 to your computer and use it in GitHub Desktop.
Save dylan-chong/17f92595fd9510abf56f405da8847421 to your computer and use it in GitHub Desktop.
defprotocol P do
def x(p)
end
defimpl P, for: Integer do
def x(p) do
p + 1
end
end
defimpl P, for: M do
end
defimpl P, for: Any do
def x(p) do
p
end
end
defmodule M do
@derive [P]
defstruct [:a]
end
P.x 2 # => 3
P.x "a" # => Error Not implemetned for string
P.x %M{} # => %M{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment