Skip to content

Commit 85e2cae

Browse files
committed
adjust formatting
1 parent b5fca80 commit 85e2cae

File tree

1 file changed

+84
-18
lines changed

1 file changed

+84
-18
lines changed

_overviews/scala3-book/taste-modeling.md

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -229,25 +229,59 @@ I didn’t include that because I didn’t know if enums are intended
229229
to replace the Scala2 “sealed trait + case class” pattern. How to resolve?
230230
{% endcomment %}
231231

232-
When writing code in an FP style, you’ll use these constructs:
232+
When writing code in an FP style, you’ll use these concepts:
233233

234-
- Enums to define ADTs
235-
- Case classes
236-
- Traits
234+
- Algebraic Data Types to define the data
235+
- Traits for functionality on the data.
237236

238-
### Enums
237+
### Enumerations and Sum Types
238+
239+
Sum types are one way to model algebraic data types (ADTs) in Scala.
240+
241+
They are used when data can be represented with different choices.
239242

240-
The `enum` construct is a great way to model algebraic data types (ADTs) in Scala 3.
241243
For instance, a pizza has three main attributes:
242244

243245
- Crust size
244246
- Crust type
245247
- Toppings
246248

247-
These are concisely modeled with enums:
249+
These are concisely modeled with enumerations, which are sum types that only contain singleton values:
250+
251+
{% tabs enum_1 class=tabs-scala-version %}
252+
{% tab 'Scala 2' for=enum_1 %}
253+
254+
In Scala 2 `sealed` classes and `case object` are combined to define an enumeration:
255+
256+
```scala
257+
sealed abstract class CrustSize
258+
object CrustSize {
259+
case object Small extends CrustSize
260+
case object Medium extends CrustSize
261+
case object Large extends CrustSize
262+
}
263+
264+
sealed abstract class CrustType
265+
object CrustType {
266+
case object Thin extends CrustType
267+
case object Thick extends CrustType
268+
case object Regular extends CrustType
269+
}
270+
271+
sealed abstract class Topping
272+
object Topping {
273+
case object Cheese extends Topping
274+
case object Pepperoni extends Topping
275+
case object BlackOlives extends Topping
276+
case object GreenOlives extends Topping
277+
case object Onions extends Topping
278+
}
279+
```
280+
281+
{% endtab %}
282+
{% tab 'Scala 3' for=enum_1 %}
248283

249-
{% tabs enum_1 %}
250-
{% tab 'Scala 3 Only' for=enum_1 %}
284+
Scala 3 offers the `enum` construct for defining enumerations:
251285

252286
```scala
253287
enum CrustSize:
@@ -263,10 +297,28 @@ enum Topping:
263297
{% endtab %}
264298
{% endtabs %}
265299

266-
Once you have an enum you can use it in all of the ways you normally use a trait, class, or object:
300+
Once you have an enumeration you can import its members as ordinary values:
301+
302+
{% tabs enum_2 class=tabs-scala-version %}
303+
{% tab 'Scala 2' for=enum_2 %}
304+
305+
```scala
306+
import CrustSize._
307+
val currentCrustSize = Small
308+
309+
// enums in a `match` expression
310+
currentCrustSize match {
311+
case Small => println("Small crust size")
312+
case Medium => println("Medium crust size")
313+
case Large => println("Large crust size")
314+
}
315+
316+
// enums in an `if` statement
317+
if (currentCrustSize == Small) println("Small crust size")
318+
```
267319

268-
{% tabs enum_2 %}
269-
{% tab 'Scala 3 Only' for=enum_2 %}
320+
{% endtab %}
321+
{% tab 'Scala 3' for=enum_2 %}
270322

271323
```scala
272324
import CrustSize.*
@@ -285,25 +337,39 @@ if currentCrustSize == Small then println("Small crust size")
285337
{% endtab %}
286338
{% endtabs %}
287339

288-
Here’s another example of how to create and use an ADT with Scala:
340+
Here’s another example of how to create a sum type with Scala, this would not be called an enumeration because the `Succ` case has parameters:
289341

290-
{% tabs enum_3 %}
291-
{% tab 'Scala 3 Only' for=enum_3 %}
342+
{% tabs enum_3 class=tabs-scala-version %}
343+
{% tab 'Scala 2' for=enum_3 %}
344+
345+
```scala
346+
sealed abstract class Nat
347+
object Nat {
348+
case object Zero extends Nat
349+
case class Succ(pred: Nat) extends Nat
350+
}
351+
```
352+
353+
Sum Types are covered in detail in the [Domain Modeling]({% link _overviews/scala3-book/domain-modeling-tools.md %}) section of this book.
354+
355+
{% endtab %}
356+
{% tab 'Scala 3' for=enum_3 %}
292357

293358
```scala
294359
enum Nat:
295360
case Zero
296361
case Succ(pred: Nat)
297362
```
298363

364+
Enums are covered in detail in the [Domain Modeling]({% link _overviews/scala3-book/domain-modeling-tools.md %}) section of this book, and in the [Reference documentation]({{ site.scala3ref }}/enums/enums.html).
365+
299366
{% endtab %}
300367
{% endtabs %}
301368

302-
Enums are covered in detail in the [Domain Modeling][data-1] section of this book, and in the [Reference documentation]({{ site.scala3ref }}/enums/enums.html).
369+
### Product Types
303370

304-
### Case classes
371+
A product type is an algebraic data type (ADT) that only has one shape, for example a singleton object, represented in Scala by a `case` object; or an immutable structure with accessible fields, represented by a `case` class.
305372

306-
The Scala `case` class lets you model concepts with immutable data structures.
307373
A `case` class has all of the functionality of a `class`, and also has additional features baked in that make them useful for functional programming.
308374
When the compiler sees the `case` keyword in front of a `class` it has these effects and benefits:
309375

0 commit comments

Comments
 (0)