Improve opaque types

This can be written a bit more concisely:

object data:
  opaque type Foo = String
  def Foo(v: String): Foo = v
  extension (x: Foo) def value: String = x
end data

As many people have expressed in the other thread, the cost of writing one extra line for a wrapper function, and one extra line for a unwrapping function is lower than the cost of having to suppress these functions when they are not wanted.

Opaque types is an advanced feature, and while forcing users to write wrappers and unwrappers may be perceived as boilerplate, it’s actually more user friendly since it forces users to understand what’s going on, and is generally a better fit with the current semantics.

This would absolutely be possible! I actually proposed it here:

As I already noted in that post however, this would just be convenient sugar. You can already derive instances like this:

object data:
  opaque type Foo = String
  def Foo(v: String): Foo = v
  extension (x: Foo) def value: String = x

  given (using ev: Eq[String]): Eq[Foo] = ev
  given (using ev: Show[String]): Show[Foo] = ev
6 Likes