Proposal for Opaque Type Aliases

An opaque type alias is almost always accompanied by a companion object, which can use extension methods to enhance the opaque type alias with relevant methods:

I personally find this a pretty roundabout way of doing the same thing that value classes do: creating a bunch of instance methods that compile down to static functions operating on the unboxed type. Would it be possible to have a more value-class-like syntax?

object JSTypes {
  opaque type LargeInt = Double {
    def toDouble: Double = this

    def +(y: LargeInt): LargeInt = this.toDouble + x.toDouble
    ...
  }
  object LargeInt {
    def fromInt(x: Int): LargeInt = x.toDouble
  }
}

Otherwise it seems odd and boilerplatey to have a bunch of extension methods that aren’t really extension methods: they’re just the primary set of methods of a particular type. The fact that the type is an opaque alias and the methods compile down to static functions isn’t really relevant.

8 Likes