Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/9ccd09a197395a51f84d415eb0e60833 to your computer and use it in GitHub Desktop.
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/efa93f2e9288e77e85fbe37fb16ffa48c0a60146
// 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.1.1"
//> 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