Are there any ways to merge two tuples in Scala 2 that is equivalenet to
in Scala 3 Tuple - Scala3
I can do this to concatenate two tuples in Scala 3:
val tup1 = (1, 2)
val tup2 = ("a", 6)
val tup3 = tup1 tup2
How can I do this in Scala 2
CodePudding user response:
You can add an extension method to Tuple2
implicit class Tuple2Ops[A0, A1](val tup: Tuple2[A0, A1]) extends AnyVal {
def [A2, A3](tup: Tuple2[A2, A3]): Tuple4[A0, A1, A2, A3] =
(this.tup._1, this.tup._2, tup._1, tup._2)
}
(1, 2) (3, 4) // (1, 2, 3, 4)
CodePudding user response:
You can do this with a generic programming library like Shapeless:
import shapeless.syntax.std.tuple._
object Main extends App {
val tup = (1, "Two") (3.0, "4", true)
println(tup) //(1,Two,3.0,4,true)
}