Skip to content

Commit 849e6a6

Browse files
authored
Rename Points to Score
Points suggests a collection of {x,y} values.
1 parent a2c60f6 commit 849e6a6

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

language/Type-Classes.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,33 +170,33 @@ Currently, instances for the following classes can be derived by the compiler:
170170

171171
If you would like your newtype to defer to the instance that the underlying type uses for a given class, then you can use newtype deriving via the `derive newtype` keywords.
172172

173-
For example, let's say you want to add two `Points` values using the `Semiring` instance of the wrapped `Int`.
173+
For example, let's say you want to add two `Score` values using the `Semiring` instance of the wrapped `Int`.
174174

175175
```purs
176-
newtype Points = Points Int
176+
newtype Score = Score Int
177177
178-
derive newtype instance semiringPoints :: Semiring Points
178+
derive newtype instance semiringScore :: Semiring Score
179179
180-
tenPoints :: Points
181-
tenPoints = (Points 4) + (Points 6)
180+
tenPoints :: Score
181+
tenPoints = (Score 4) + (Score 6)
182182
```
183183

184184
That `derive` line replaced all this code!:
185185

186186
```purs
187187
-- No need to write this
188-
instance semiringPoints :: Semiring Points where
189-
zero = Points 0
190-
add (Points a) (Points b) = Points (a + b)
191-
mul (Points a) (Points b) = Points (a * b)
192-
one = Points 1
188+
instance semiringScore :: Semiring Score where
189+
zero = Score 0
190+
add (Score a) (Score b) = Score (a + b)
191+
mul (Score a) (Score b) = Score (a * b)
192+
one = Score 1
193193
```
194194

195195
Note that we can use either of these options to derive an `Eq` instance for a `newtype`, since `Eq` has built-in compiler support. They are equivalent in this case.
196196

197197
```purs
198-
derive instance eqPoints :: Eq Points
199-
derive newtype instance eqPoints :: Eq Points
198+
derive instance eqScore :: Eq Score
199+
derive newtype instance eqScore :: Eq Score
200200
```
201201

202202
### Deriving from `Generic`
@@ -227,12 +227,12 @@ Note that the `show` output string can be copy-pasted to reconstruct the origina
227227
```purs
228228
import Effect.Console (logShow)
229229
230-
newtype Points = Points Int
230+
newtype Score = Score Int
231231
232232
-- newtype deriving omits wrapper with show
233-
derive newtype instance showPoints :: Show Points
233+
derive newtype instance showScore :: Show Score
234234
235-
main = logShow (Points 5)
235+
main = logShow (Score 5)
236236
-- Prints:
237237
-- 5
238238
```
@@ -242,16 +242,16 @@ import Data.Generic.Rep (class Generic)
242242
import Data.Generic.Rep.Show (genericShow)
243243
import Effect.Console (logShow)
244244
245-
newtype Points = Points Int
245+
newtype Score = Score Int
246246
247247
-- generic deriving prints wrapper with show
248-
derive instance genericPoints :: Generic Points _
249-
instance showPoints :: Show Points where
248+
derive instance genericScore :: Generic Score _
249+
instance showScore :: Show Score where
250250
show = genericShow
251251
252-
main = logShow (Points 5)
252+
main = logShow (Score 5)
253253
-- Prints:
254-
-- (Points 5)
254+
-- (Score 5)
255255
```
256256

257257
More information on Generic deriving is available [in the generics-rep library documentation](https://pursuit.purescript.org/packages/purescript-generics-rep). See this [blog post](https://harry.garrood.me/blog/write-your-own-generics/) for a tutorial on how to write your own `generic` functions.

0 commit comments

Comments
 (0)