I have a trait with objects that Im using to represent an enum like:
sealed trait status extends Product with Serializable
object status{
case object pending extends status
case object ready extends status
type ready = ready.type
type pending = pending.type
}
Then I have two case classes:
case class container[+S <: Status](status : S, commonValue: String)
case class notAContainer(status : Status, commonValue:String)
I want to be able to map my notAContainer class to a container class by using the value it has in Status. Is there anyway I can do that? I could also change the type of status within notAContainer.
You can just do
You will have values of type
container[status](not of its subtypescontainer[status.pending],container[status.ready]).Just in case, if it doesn't suit your use case please explain why (why you need values of types
container[status.pending],container[status.ready], how you're going to use them etc.).If this is really important (for example if the constructor of class
containerbehaves differently for differentS) then for example you can specify the type parameter and downcastOr you can use pattern matching
But the result will have type
container[status].You can even automate the pattern matching with a macro
Pattern matching (manual or with the macro) occurs at runtime. So at compile time we can't have a value of types
container[status.pending],container[status.ready], only a value of typecontainer[status].If you really need a value of type
container[status.pending]orcontainer[status.ready]then you can use reflective compilation at runtime(*) 1 2
Inside quasiquotes
q"..."the variablechas typecontainer[status.pending].