Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 604 Bytes

4-data-definitions.md

File metadata and controls

20 lines (17 loc) · 604 Bytes
optionId scastieLink codeTitle description
data-definitions
Define data types and pattern match on them with ease
Model domains precisely, and make choices based on the shape of data.
enum Payment:
  case Card(name: String, digits: Long, expires: Date)
  case PayPal(email: String)

def process(kind: Payment) = kind match
  case Card(name, digits, expires) =>
    s"Processing credit card $name, $digits, $expires"
  case PayPal(email) =>
    s"Processing PayPal account $email"

process(Card(name, digits, expires))