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


#1

Hi,

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 = {
    containerInstance.embeddedInstance
  }
}

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.


#2

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.


#3

ok, thanks I’ll try to do that