Skip to content

Commit c98df7b

Browse files
committed
Correct and simplify Display example
Closes #263
1 parent 91d7eaf commit c98df7b

File tree

1 file changed

+1
-42
lines changed

1 file changed

+1
-42
lines changed

src/pages/type-classes/display.md

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -164,63 +164,22 @@ by adding extension methods for its functionality:
164164
you created in the previous exercise.
165165

166166
<div class="solution">
167-
```scala mdoc:reset:invisible
168-
trait Display[A] {
169-
def display(value: A): String
170-
def print(value: A): Unit =
171-
println(display(value))
172-
}
173-
```
174-
175167
First we define `DisplaySyntax` with the extension methods we want.
176168

177169
```scala mdoc:silent
178170
object DisplaySyntax {
179171
extension [A](value: A)(using p: Display[A]) {
180172
def display: String = p.display(value)
181-
def print: Unit = p.print(value)
173+
def print: Unit = Display.print(value)
182174
}
183175
}
184176
```
185177

186178
Now we can show everything working by calling `print` on a `Cat`.
187179

188-
```scala mdoc:reset:invisible
189-
trait Display[A] {
190-
def display(value: A): String
191-
def print(value: A): Unit =
192-
println(display(value))
193-
}
194-
object Display {
195-
given Display[String] with {
196-
def display(value: String) = value
197-
}
198-
199-
given Display[Int] with {
200-
def display(value: Int) = value.toString
201-
}
202-
}
203-
204-
object DisplaySyntax {
205-
extension [A](value: A)(using p: Display[A]) {
206-
def display: String = p.display(value)
207-
def print: Unit = println(value.display)
208-
}
209-
}
210-
final case class Cat(name: String, age: Int, color: String)
211-
```
212180
```scala mdoc
213181
import DisplaySyntax.*
214182

215-
given Display[Cat] with {
216-
def display(cat: Cat): String = {
217-
val name = cat.name.display
218-
val age = cat.age.display
219-
val color = cat.color.display
220-
s"$name is a $age year-old $color cat."
221-
}
222-
}
223-
224183
Cat("Garfield", 41, "ginger and black").print
225184
```
226185

0 commit comments

Comments
 (0)