Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a type as a parameter in scala?

I'm having a really hard time trying to figure out how to 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,


EDIT as requested

Suppose I have an Array[Byte] = A973928CB3883FB123 named Command1

The data in this bytes is fixed in position and length. In other words, I know that position 1-4 is for example a small date, the 5-9 is the name of a customer, etc etc.

What I want is to write a single parsing function that takes as only parammeter a schema and returns the actual values of every param in the schema.

trait Command{

  //This is implemented in every command
  val schema : List[Tuple[String,Int,Int,Type]]  //Position,Size,DataType

  def parse() : List[Tuple[String,Int,Int,Type,Any]] = schema.map(//match using the type)
}

class Command1 extends Command {
  override val schema = List[Tuple("theName",0,10,String),Tuple("myType",10,12,MyType),Tuple("theId",13,20,Long)]
  val theActualName = parse().find(_._1 == "theName")._5.asInstanceOf[String] //I would like to avoid this cast

}

I hope this clarify what I'm trying to do.

like image 838
rsan Avatar asked Sep 04 '12 21:09

rsan


People also ask

What is type parameter in Scala?

Language. Methods in Scala can be parameterized by type as well as value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses.

What does => mean in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .

How do I pass an argument to a Scala object?

you can access your Scala command-line arguments using the args array, which is made available to you implicitly when you extend App . As an example, your code will look like this: object Foo extends App { if (args. length == 0) { println("dude, i need at least one parameter") } val filename = args(0) ... }

What is classOf in Scala?

A classOf[T] is a value of type Class[T] . In other words, classOf[T]: Class[T] . For example: scala> val strClass = classOf[String] strClass: Class[String] = class java. lang. String scala> :t strClass Class[String]

What is [+ A in Scala?

It declares the class to be covariant in its generic parameter. For your example, it means that Option[T] is a subtype of Option[S] if T is a subtype of S . So, for example, Option[String] is a subtype of Option[Object] , allowing you to do: val x: Option[String] = Some("a") val y: Option[Object] = x.


1 Answers

You can get types as values with Scala 2.10 (when it comes out). Before that, the best you can get is the class (classOf[String], for example), but that loses any type parameter.

Even with Scala 2.10 there are some serious limitations you'll come up against. See a recent blog post of mine for an example.

like image 70
Daniel C. Sobral Avatar answered Sep 21 '22 01:09

Daniel C. Sobral