How can I pass a type as a parameter in scala?
- by rsan
I'm having a really hard time trying to figure out how can I store or pass a type in scala.
What I want to achive is something like this:
abstract class Foo( val theType : type )
object Foo{
case object Foo1 extends Foo(String)
case object Foo2 extends Foo(Long)
}
So at some point I can do this:
theFoo match{
case String => "Is a string"
case Long => "Is a long"
}
and when obtaining the object being able to cast it:
theFoo.asInstanceOf[Foo1.theType]
Is this possible? If is possible, is a good aproach? What I'm trying to achieve ultimately is writing a pseudo schema for byte stream treatment. E.g if I have an schema Array(Foo1,Foo1,Foo2,Foo3,Foo1) I could parse Arrays of bytes that complain with that schema, if at some point I have a different stream of bytes I could just write a new schema Array(Foo3, Foo4, Foo5) without having to reimplement parsing logic.
Regards,