Created
October 21, 2015 18:19
-
-
Save yltsrc/349af3bd888a0b8c462e 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
# part of my library | |
defmodule FooLib do | |
defprotocol BarProtocol do | |
def bar(value) | |
end | |
defimpl BarProtocol, for: List do | |
def bar([h | _rest]), do: h | |
end | |
defimpl BarProtocol, for: Tuple do | |
def bar({h, _rest}), do: h | |
end | |
@spec foo(list) :: any | |
@spec foo(tuple) :: any | |
# how can I define spec for protocol? | |
def foo(value) do | |
BarProtocol.bar(value) | |
end | |
end | |
# part of 3rd party library, which implements BarProtocol | |
defmodule BazStruct do | |
defstruct bar: "buzz" | |
end | |
defimpl BarProtocol, for: BazStruct do | |
def bar(%BazStruct{bar: value}), do: value | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment