Opaque types

Magda Stożek

Get more clarity with

The perfect model

  • Precise
  • Readable
  • Prevents mistakes

Scala 2

  • Type aliases
  • Refined types
  • Value classes
type Age = Int
case class Age(value: Int) extends AnyVal
type Age = Int Refined Positive

Opaque type aliases

Demo:

  • why "opaque"
  • how to add API
    • object methods
    • instance methods
  • bounds

Newtypes

Refined types

type Pages = Int
Pages(350)
if (value > 0) Some(value) else None

Ecosystem

Opaque types + Iron

Demo

Opaque types summary

 

Advantages:

  • simple and clear
  • no overhead
  • opaque

 

Disadvantages:

  • no API (*unless bounds)
  • no pattern matching
  • no toString/equals overriding

Read more

Questions?

  opaque type EndOfPresentation = TimeToAskQuestions
http://bit.ly/magda-scalalove

Opaque types in Scala 3 [40 min]

By Magda Stożek

Opaque types in Scala 3 [40 min]

  • 46