Skip to content

Instantly share code, notes, and snippets.

@lachezar
Last active November 30, 2020 16:07
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 lachezar/388e9c752e79109c4913da506c640572 to your computer and use it in GitHub Desktop.
Save lachezar/388e9c752e79109c4913da506c640572 to your computer and use it in GitHub Desktop.
currency_example.scala
sealed trait Currency
case object Dollar extends Currency
case object Euro extends Currency
case class Money[T <: Currency](value: Int, currency: T) {
def +(that: Money[T]): Money[T] = {
Money[T](this.value + that.value, currency)
}
}
val a = Money(5, Dollar)
val b = Money(5, Euro)
val c = Money(3, Dollar)
val d = Money(10, Euro)
// a + b - does not compile!
println(a + c)
println(b + d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment