user2051561
user2051561

Reputation: 837

Flattening arbitrarily nested tuples containing case class elements using Shapeless

I used the following example:

https://github.com/milessabin/shapeless/blob/master/examples/src/main/scala/shapeless/examples/flatten.scala

to flatten tuples. However I have now realized that case classes are also flattened. Is their any way I can ensure that only the tuples are flattened?

TIA

Upvotes: 4

Views: 333

Answers (1)

devkat
devkat

Reputation: 1644

You could require an implicit IsTuple:

object flatten extends LowPriorityFlatten {
  implicit def caseTuple[P <: Product : IsTuple]
      (implicit lfm: Lazy[FlatMapper[P, flatten.type]]) =
    at[P](lfm.value(_))
}

Test:

val v4 = (Bar(Foo("a")), (true, 2.0, "foo"))
val f4 = flatten(v4)
typed[(Bar, Boolean, Double, String)](f4)

Upvotes: 4

Related Questions