سؤال

I have a case class with a property location, which is a tuple2 instance containing doubles.

I first convert it to a list using the following:

testPlace.location.productIterator.toList

This produces a two element list,

List(78.0342, -139.2234)

Running a simple map across this list seems to fail, raising the following compilation error:

loc.map((x:Double) => x + 10.0)

 type mismatch;
   found   : Double => Double
   required: Any => ?
          loc.map( (x:Double) => x + 10.0 )

Can someone explain the rationale behind this and what is wrong with my syntax? What is that required type signature trying to tell me?

هل كانت مفيدة؟

المحلول

Product2.productIterator returns Iterator[Any], so the actual type of your list is List[Any]. This is why your map function is expecting a Any => ?.

http://www.scala-lang.org/api/current/index.html#scala.Product2

نصائح أخرى

Use pattern matching (case)

loc map {case (x:Double) => x + 10.0}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top