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
+3-9
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 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.
90
+
Protocols are a mechanism to achieve polymorphism in Elixir. Dispatching on a protocol is available to any data type as long as it implements the protocol. Lets 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
@@ -174,7 +174,7 @@ Now all data types that we have not implemented the `Blank` protocol for will be
174
174
175
175
### 4.2.2 Using protocols with records
176
176
177
-
The power of Elixir extensibility comes when protocols and records are mixed.
177
+
The power of Elixir's extensibility comes when protocols and records are mixed.
178
178
179
179
For instance, Elixir provides a `HashDict` implementation that is an efficient data structure to store many keys. Let's take a look at how it works:
180
180
@@ -207,13 +207,7 @@ Blank.blank?(dict) #=> false
207
207
Blank.blank?(HashDict.new) #=> true
208
208
```
209
209
210
-
Excellent! The best of all is that we implemented the `Blank` protocol for an existing data structure (`HashDict`) without a need to wrap it or recompile it, which allows developers to easily extend previously defined protocols. Note this only worked because, when we defined the protocol, we have added `Record` to the list of types supported by the protocol:
211
-
212
-
```elixir
213
-
@only [Atom, Record, Tuple, List, BitString, Any]
214
-
```
215
-
216
-
Keep in mind that `Record` needs to come before `Tuple`, since all records are tuples (but not all tuples are records). For this reason, in case a record does not implement a given protocol, Elixir will fall back to the tuple implementation of that protocol if one exists. So one can add a default protocol implementation for all records by simply defining a default implementation for tuples.
210
+
Excellent! The best of all is that we implemented the `Blank` protocol for an existing data structure (`HashDict`) without a need to wrap it or recompile it, which allows developers to easily extend previously defined protocols.
0 commit comments