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)