O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Available Types

You can define implementations for one or more of the following types:

Any

Atom

BitString

Float

Function

Integer

List

PID

Port

Record

Reference

Tuple

The type BitString is used in place of Binary.

The type Any is a catchall, allowing you to match an implementation with any type. Just as with function definitions, you’ll want to put the implementations for specific types before an implementation for Any.

You can list multiple types on a single defimpl. For example, the following protocol can be called to determine if a type is a collection:

protocols/is_collection.exs
 
defprotocol​ Collection ​do
 
@fallback_to_any​ ​true
 
def​ is_collection?(value)
 
end
 
 
defimpl​ Collection, ​for​: [List, Tuple, BitString] ​do
 
def​ is_collection?(_), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required