Scala中的参数列表中是否直接支持元组拆包?
发布时间:2020-12-16 19:02:29 所属栏目:安全 来源:网络整理
导读:在哈斯克尔你可以写: x :: (Int,Int) - Intx (p,s) = p 在斯卡拉你会写: def x(a: (Int,Int)) = a._1 要么: def x(a: (Int,Int)) = a match { case (p,s) = p} 为什么没有这样的东西 def x(_: (p: Int,s: Int)) = p 要么 def x(foo: (p @ Int,s @ Int)) =
在哈斯克尔你可以写:
x :: (Int,Int) -> Int x (p,s) = p 在斯卡拉你会写: def x(a: (Int,Int)) = a._1 要么: def x(a: (Int,Int)) = a match { case (p,s) => p } 为什么没有这样的东西 def x(_: (p: Int,s: Int)) = p 要么 def x(foo: (p @ Int,s @ Int)) = p ? 解决方法
您正在寻找的功能称为解构,而且它的一般形式将远远超出元组解包.我经常发现自己希望Scala有它,因为它是模式匹配语法的自然扩展:
def first((f: Int,l: Int)) = f def displayName(Person(first,last)) = last + "," + first 解构是以(变量)形式存在的变量/值定义: val (f,l) = tuple val Person(first,last) = person 不幸的是,有一些这样的定义,我认为,不太可能会在参数列表中看到解构. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |