Last active
June 8, 2017 11:22
-
-
Save johanandren/87fec0b627996a3f850513b81e0a8d66 to your computer and use it in GitHub Desktop.
Sample for SO question https://stackoverflow.com/questions/1094173/how-do-i-get-around-type-erasure-on-scala-or-why-cant-i-get-the-type-paramete#21640639
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package basics | |
import akka.actor.{ActorSystem, ExtendedActorSystem} | |
import akka.serialization.{JavaSerializer, SerializationExtension} | |
import com.typesafe.config.ConfigFactory | |
trait MyPayload | |
trait OtherPayload | |
trait PersistentExecutorProtocol extends Serializable { | |
type Payload | |
sealed trait Event extends Serializable | |
sealed trait Todo { def value: Payload } | |
final case class Done(value: Long) extends Event | |
} | |
object AProtocolInstance extends PersistentExecutorProtocol { | |
type Payload = MyPayload | |
final case class ToDo(value: MyPayload) extends super.Todo | |
} | |
object BProtocolInstance extends PersistentExecutorProtocol { | |
type Payload = OtherPayload | |
final case class ToDo(value: OtherPayload) extends super.Todo | |
} | |
class SerializerA(system: ExtendedActorSystem) extends JavaSerializer(system) | |
class SerializerB(system: ExtendedActorSystem) extends JavaSerializer(system) | |
object SerializerAndPathDependentTypes extends App { | |
implicit val system = ActorSystem("sample", ConfigFactory.parseString( | |
""" | |
akka.actor { | |
serializers { | |
serializerA = "basics.SerializerA" | |
serializerB = "basics.SerializerB" | |
} | |
serialization-identifiers { | |
"basics.SerializerA" = 70 | |
"basics.SerializerB" = 71 | |
} | |
serialization-bindings { | |
"basics.AProtocolInstance$ToDo" = serializerA | |
"basics.BProtocolInstance$ToDo" = serializerB | |
} | |
} | |
""")) | |
val ser = SerializationExtension(system) | |
println("serializer for AProtocolInstance.ToDo: " + ser.serializerFor(classOf[AProtocolInstance.ToDo]).getClass) | |
println("serializer for BProtocolInstance.ToDo: " + ser.serializerFor(classOf[BProtocolInstance.ToDo]).getClass) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment