Dotty Type classes

type class Semigroup[A] {
  extension def combine(this x: A)(y: A): A
}

type class Monoid[A] : Semigroup[A] {
  def empty: A
}

If a more conservative syntax is used, we might be able to do this with

trait Semigroup[A] extends Typeclass {
  def combine(@this x: A)(y: A): A
}

trait Monoid[A: Semigroup] extends Typeclass {
  def empty: A
}

in Scala2 with scalaz-plugin (https://github.com/scalaz/scalaz-plugin).

5 Likes