如何指定元组类型签名

How to specify tuple type signature

我正在学习“学习 Scala”这本书,并且已经完成了第 4 章中的这个练习:

Write a function that takes a 3-sized tuple and returns a 6-sized tuple, with each original parameter followed by its String representation. For example, invoking the function with (true, 22.25, "yes") should return (true, "true", 22.5, "22.5", "yes", "yes").

我指定类型签名的尝试中最有希望的是:

def t3ToT6[Tuple3[A, B, C]](t: Tuple3[A, B, C]) = {

产生这些错误:

Exercises.scala:99: error: not found: type A
  def t3ToT6[Tuple3[A, B, C]](t: Tuple3[A, B, C]) = {
                                        ^
Exercises.scala:99: error: not found: type B
  def t3ToT6[Tuple3[A, B, C]](t: Tuple3[A, B, C]) = {
                                           ^
Exercises.scala:99: error: not found: type C
  def t3ToT6[Tuple3[A, B, C]](t: Tuple3[A, B, C]) = {
                                              ^

目的是允许任何类型,因此我可以用 ABC?

来表示那些类型

如果我取消类型说明而只使用 Tuple3,那么它会抱怨我 应该 有类型参数:

Exercises.scala:99: error: type Tuple3 takes type parameters
  def t3ToT6[Tuple3[A, B, C]](t: Tuple3) = {

我怀疑我很接近,这是某种语法问题,但尚未找到任何在函数类型签名中指定元组的示例。

此问题描述的正确类型签名是什么?

有没有你知道但我还没有找到的例子可以帮助我理解这一点?

像这样识别 3 个类型参数:[A,B,C]

有了这个你可以...

def t3ToT6[A,B,C](t: Tuple3[A,B,C]):Tuple6[A,String,B,String,C,String] =
  Tuple6(t._1, t._1.toString
        ,t._2, t._2.toString
        ,t._3, t._3.toString)

也可以通过快速模式匹配来完成。

def t3ToT6[A,B,C](t:(A,B,C)):(A,String,B,String,C,String) = t match {
  case (a,b,c) => (a, a.toString, b, b.toString, c, c.toString)
}