Reputation: 595
I trying convert List[Any] to tuple with some data types.
def matchValue(list: List[Any]):(Int, Int, Int, Option[String], String,Option[Date],String, Date,String, Option[Int],Option[String])= {
list match {
case i1::i2::i3::i4::i5::i6::i7::i8::i9::i10::i11 => (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11)
}
}
but i have error:
Expression of type Any doesn't conform to expected type Int
Upvotes: 0
Views: 404
Reputation: 484
You could try to convert each value to its primitive type using x.asInstanceOf[T]
. So for example x.asIntanceOf[Int]
will convert any x
to an Int
.
Upvotes: 0
Reputation: 170713
You can write
case (i1: Int) :: (i2: Int) :: // etc
If you have many similar cases with different target tuples, you indeed want something like shapeless (same tuple sizes can be handled).
: Option[String]
pattern can actually only check the argument is an Option
. You'll get a warning about this, which can be ignored by using Option[String @unchecked]
, but it should only be done if you are sure you'll really get an Option[String]
.
Upvotes: 1