Containers with embedded types (and "moving around" embedded type instances)


I’m trying to understand how to use embedded types and create derivatives from them, and I can’t understand why the following code produces errors:

trait Container {
  type EmbeddedType

  val embeddedInstance : EmbeddedType

trait ContainerDerivative {
  val targetContainer : Container
  val embeddedInstanceOfTarget : targetContainer.EmbeddedType

val containerInstance : Container = ???

new ContainerDerivative {
  override val targetContainer: Container = containerInstance

  // ERROR: not enough arguments for implicitly
  implicitly[targetContainer.EmbeddedType =:= containerInstance.EmbeddedType]
  // ERROR: type mismatch
  override val instanceOfContainerEmbedded: targetContainer.EmbeddedType = {

I didn’t expect the errors - I expected scala to correctly resolve the types, but that doesn’t happen.

Is there an explanation anywhere on the semantics/mechanics of embedded types?

The context for this is a DFA where the states should be “opaque” to the user - but they are still supposed to be passed around by the user.

You need to use singleton type equivalence. In other words, the ascription:

override val targetContainer: containerInstance.type = ...

This makes it so targetContainer.typecontainerInstance.type, and so on.

ok, thanks I’ll try to do that