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

Summary

 

Advantages:

  • simple and clear
  • no overhead
  • opaque

 

Disadvantages:

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

Read more

Questions?

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

Opaque types in Scala 3

By Magda Stożek

Opaque types in Scala 3

  • 316