You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: getting_started/4.markdown
+10-12
Original file line number
Diff line number
Diff line change
@@ -87,7 +87,7 @@ For more information on records, [check out the documentation for the `defrecord
87
87
88
88
## 4.2 Protocols
89
89
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.
91
91
92
92
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.
93
93
@@ -103,8 +103,8 @@ end
103
103
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:
104
104
105
105
```elixir
106
-
#Numbers are never blank
107
-
defimplBlank, for: Numberdo
106
+
#Integers are never blank
107
+
defimplBlank, for: Integerdo
108
108
defblank?(_), do:false
109
109
end
110
110
@@ -129,7 +129,8 @@ And we would do so for all native data types. The types available are:
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
155
156
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:
159
158
160
159
```elixir
161
160
defprotocolBlankdo
162
-
@only [Atom, Record, Tuple, List, BitString, Any]
161
+
@fallback_to_anytrue
163
162
defblank?(data)
164
163
end
165
164
```
166
165
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 implementedas:
168
167
169
168
```elixir
170
169
defimplBlank, for: Anydo
171
170
defblank?(_), do:false
172
171
end
173
172
```
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.
0 commit comments