How to apply implicit conversions between tuples?

喜欢而已 提交于 2019-12-07 21:47:30

Q1: Why the tuple conversion doesn't work? (I'm expecting that an answer to that goes in the lines of: What's required is an implicit conversion between Tuple2[Cont,Cont] to Tuple2[String,Int] but that does not scale up)

Yes, you've got that right.

What are my options to have such conversion working?

You could do it this way:

implicit def liftImplicitTuple2[A, B, A1, B1](tuple: (A, B))
  (implicit f1: A => A1, f2: B => B1): (A1, B1) =
  (f1(tuple._1), f2(tuple._2))

implicit def liftImplicitTuple3[A, B, C, A1, B1, C1](tuple: (A, B, C))
  (implicit f1: A => A1, f2: B => B1, f3: C => C1): (A1, B1, C1) =
  (f1(tuple._1), f2(tuple._2), f3(tuple._3))

// etc for tuples as large as you need
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!