Skip to content

Ergonomics of opaque types and delegates #6716

Closed
@milessabin

Description

@milessabin

An opaque type and its underlying type have exactly the same representation, so it should be possible to opt to directly reuse delegates for the underlying as delegates for the opaque type. This is pretty much the gist of Andres Loh's Deriving Via paper.

Ideally we'd be able to write something like,

trait Monad[T]
class Foo
object Foo {
  delegate for Monad[Foo]
}

opaque type Bar = Foo
object Bar {
  delegate for Monad[Bar] = the[Monad[Foo]]
}

object Test {
  val mf = the[Monad[Foo]]
  val mb = the[Monad[Bar]]
}

But this doesn't work currently, at least in part because recent changes to opaque types mean they no longer have an implicit scope of their own. The closest I think we can get right now is something like,

trait Monad[T]
class Foo
object Foo {
  delegate for Monad[Foo]
}

opaque type Bar = Foo
delegate for Monad[Bar] = the[Monad[Foo]].asInstanceOf

object Test {
  val mf = the[Monad[Foo]]
  val mb = the[Monad[Bar]]
}

which has an ugly (and surely unnecessary?) cast and an untidy top-level delegate definition.

Can we do any better?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions