我发现自己想要避免元组只是因为访问语法很难看.鉴于arity是有限的,为什么Scala不支持更好的语法,看起来更好,更容易键入?这是我的建议:
val t = (1,2,3)
// Proposed equivalent reference syntax.
assert(t.a == t._1)
assert(t.b == t._2)
assert(t.c == t._3)
Run Code Online (Sandbox Code Playgroud)
那么,我想要优雅,还是只是愚蠢?
通常您不需要使用_1等方法,因为您可以进行模式匹配:
val t = (1,2,3)
val (a, b, c) = t
val t2 = ((1, 2), (3, 4))
t2 match {
case ((a, b), (c, d)) if a > c && b > d => true
case _ => false
}
Run Code Online (Sandbox Code Playgroud)