Question

Using shapeless and an example from the tutorial:

import shapeless._
import syntax.std.tuple._
import poly._

object ShapelessPlay extends App{

  val t = ((1,"a"),'c')

  println(t flatMap identity)
}

I get the following error:

could not find implicit value for parameter mapper: shapeless.ops.tuple.FlatMapper[((Int, String), Char),shapeless.poly.identity.type]

println(t flatMap identity)

What am I missing? ^

Was it helpful?

Solution

It fails because flatMap expects its function argument to yield a tuple (of some arity or other) when applied to each of the elements of its left hand side. identity yields a tuple when applied to (1, "a"), but not when applied to 'c' ... in the latter case it yields a Char. What you really want here is,

scala> ((1, "a"), Tuple1('c')) flatMap identity
res0: (Int, String, Char) = (1,a,c)

which admittedly isn't quite as pretty as it should be because Scala doesn't have syntax for Tuple1.

Alternatively, it you simple want to append a value to a tuple, increasing the latter's arity by one, the simplest option is to use :+,

scala> (1, "a") :+ 'c'
res0: (Int, String, Char) = (1,a,c)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top