Skip to content

Instantly share code, notes, and snippets.

@yltsrc
Created October 21, 2015 18:19
Show Gist options
  • Save yltsrc/349af3bd888a0b8c462e to your computer and use it in GitHub Desktop.
Save yltsrc/349af3bd888a0b8c462e to your computer and use it in GitHub Desktop.
# 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