Skip to content

Commit

Permalink
Add Protocol.impl_for/1 and Protocol.impl_for!/1
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 18, 2025
1 parent 83a70d7 commit c22c907
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/elixir/lib/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,49 @@ defmodule Protocol do
raise ArgumentError, "invalid arguments for def inside defprotocol"
end

@doc """
Returns the implementation of a given protocol for a given data type
or `nil`.
This wraps `protocol.impl_for(value)` with a generic type signature.
## Examples
iex> Protocol.impl_for(Enumerable, [1, 2, 3])
Enumerable.List
iex> Protocol.impl_for(Enumerable, :atom)
nil
"""
@doc since: "1.19.0"
def impl_for(protocol, value) do
protocol.impl_for(value)
end

@doc """
Returns the implementation of a given protocol for a given data type
or raises.
This wraps `protocol.impl_for!(value)` with a generic type signature.
## Examples
iex> Protocol.impl_for!(Enumerable, [1, 2, 3])
Enumerable.List
iex> try do
...> Protocol.impl_for!(Enumerable, :atom)
...> rescue
...> Protocol.UndefinedError -> :raised
...> end
:raised
"""
@doc since: "1.19.0"
def impl_for!(protocol, value) do
protocol.impl_for!(value)
end

@doc """
Checks if the given module is loaded and is protocol.
Expand Down

0 comments on commit c22c907

Please sign in to comment.