Alasca: Traits, Sealed Traits and Enum Traits

Published on 2018-08-31.


  • traits are interfaces with implementations (and type classes (todo))
trait Name
	fun name: String
  • traits can have constructor fields:
trait Age(age: Int32)

Sealed Traits

  • @sealed requires that all classes/values/modules of a trait are defined in the trait’s companion module:
trait Car(expensive: Boolean)
	fun model: String

module Car
	module NoCar                extends Car
		fun model: String = "none"
	class Jaguar(model: String) extends Car(true)
	value Nissan(model: String) extends Car(true)

Enum Traits

  • an @enum trait is more restrictive than a sealed trait: it can only contain modules
module Friend
	module Joe    extends Age(23), Friend
	module Joanna extends Age(42), Friend
	module John   extends Age(17), Friend

trait Friend
  • in return, an enum value receives some helper methods by default:
Friends.values == ImmutableArray(Joe, Joanna, John)
Friends.withName("Joe") == Friends.Joe
Set[Friend](...) // implementation uses a bit field