Last active
May 25, 2024 10:20
-
-
Save dacr/9ccd09a197395a51f84d415eb0e60833 to your computer and use it in GitHub Desktop.
scala algebric data types / published by https://github.com/dacr/code-examples-manager #991c877f-0b43-4cab-86be-4a064660407e/27d1af7560077c01cb64f01264daa65be76615e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : scala algebric data types | |
// keywords : scala, adt, algebric-data-types, language-feature, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 991c877f-0b43-4cab-86be-4a064660407e | |
// created-on : 2020-05-31T19:54:52Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.scalatest::scalatest:3.2.10" | |
// --------------------- | |
// written after [John De Goes - 12 Steps To Better Scala (Part I)](https://youtu.be/71yhnTGw0hY) | |
println( | |
""" | |
|Algebric Data Types (ATD) is made of | |
| + Product types (just simple case class) | |
| - Defines commonality | |
| + Sum types | |
| | |
|the goal of ADT is to made impossible to represent an illegal state ! | |
| | |
|[John De Goes - 12 Steps To Better Scala (Part I)](https://youtu.be/71yhnTGw0hY) | |
| | |
|""".stripMargin | |
) | |
// ---------------------------------------------------------------- | |
// Products | |
case class Person(name:String, age:Int) | |
// ---------------------------------------------------------------- | |
// Sums | |
sealed trait Contact | |
case class Email(value: String) extends Contact | |
case class Phone(value: String) extends Contact | |
println("so inheritance is not used...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment