Scala3 Tuple - iterate over elements to convert them

It seems that you cannot use Tuple.map in this case, since the converter would have to be given to the inner function as an extra parameter list and that clashes with the prototype specified by map.

You would have to do it by recursion (see Tuples bring generic programming to Scala 3 | The Scala Programming Language) like this:

trait StringConverter[T] {
  def convert(t: T): String
}

object StringConverter {
  def convert[T](t: T)(using conv: StringConverter[T]): String = conv.convert(t)
}

given StringConverter[EmptyTuple] with
  def convert(empty: EmptyTuple) =
    ""

// Inductive case
given [H: StringConverter, T <: Tuple: StringConverter]: StringConverter[H *: T] with
  def convert(tuple: H *: T) =
    StringConverter.convert(tuple.head) + StringConverter.convert(tuple.tail)

@main def main = {
  val tuple = ("abc", 123, BigDecimal("1.23"))

  println(StringConverter.convert(tuple))  // string=abcint=123big-decimal=1.23
}

Now, String concatenation might not be what you want, but you could adapt this to use a List instead, which is probably what you want anyway.

1 Like