Skip to content

Commit c660655

Browse files
committed
Updated Protocol @fallback example
1 parent ae3853f commit c660655

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

getting_started/4.markdown

+10-12
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ For more information on records, [check out the documentation for the `defrecord
8787

8888
## 4.2 Protocols
8989

90-
Protocols is a mechanism to achieve polymorphism in Elixir. Dispatching a protocol is available to any data type as long as it implements the protocol. Let's consider a practical example.
90+
Protocols are a mechanism to achieve polymorphism in Elixir. Dispatching a protocol is available to any data type as long as it implements the protocol. Let's consider a practical example.
9191

9292
In Elixir, only `false` and `nil` are treated as false. Everything else evaluates to true. Depending on the application, it may be important to specify a `blank?` protocol that returns a boolean for other data types that should be considered blank. For instance, an empty list or an empty binary could be considered blanks.
9393

@@ -103,8 +103,8 @@ end
103103
The protocol expects a function called `blank?` that receives one argument to be implemented. We can implement this protocol for some Elixir data types in the following way:
104104

105105
```elixir
106-
# Numbers are never blank
107-
defimpl Blank, for: Number do
106+
# Integers are never blank
107+
defimpl Blank, for: Integer do
108108
def blank?(_), do: false
109109
end
110110

@@ -129,7 +129,8 @@ And we would do so for all native data types. The types available are:
129129
* `Atom`
130130
* `List`
131131
* `BitString`
132-
* `Number`
132+
* `Integer`
133+
* `Float`
133134
* `Function`
134135
* `PID`
135136
* `Port`
@@ -151,28 +152,25 @@ iex> Blank.blank?("hello")
151152
** (UndefinedFunctionError) undefined function: Blank.BitString.blank?/1
152153
```
153154

154-
Implementing the protocol for all 9 types above can be cumbersome, but Elixir gives us some tools to select which protocols we want to implement explicitly.
155+
### 4.2.1 Fallback to any
155156

156-
### 4.2.1 Selecting implementations
157-
158-
Implementing the protocol for all types can be a bit of work, and in some cases even unnecessary. Going back to our `Blank` protocol, the types `Number`, `Function`, `PID`, `Port` and `Reference` are never going to be blank. To make things easier, Elixir allows us to declare the fact that we are going to implement the protocol just for some types, as follows:
157+
In some cases, it may be convenient to provide a default implementation for all types. This can be achieved by setting `@fallback_to_any` to `true` in the protocol definition:
159158

160159
```elixir
161160
defprotocol Blank do
162-
@only [Atom, Record, Tuple, List, BitString, Any]
161+
@fallback_to_any true
163162
def blank?(data)
164163
end
165164
```
166165

167-
Since we also specified `Any` as a data type, if the data type is not any of `Atom`, `Record`, `Tuple`, `List` or `BitString`, it will automatically fall back to `Any`:
166+
Which can now be implemented as:
168167

169168
```elixir
170169
defimpl Blank, for: Any do
171170
def blank?(_), do: false
172171
end
173172
```
174-
175-
Now all data types that we have not specified will be automatically considered non-blank.
173+
Now all data types that we have not implemented the `Blank` protocol for will be considered non-blank.
176174

177175
### 4.2.2 Using protocols with records
178176

0 commit comments

Comments
 (0)