Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Scala 中,如果我有一个函数 f(X, Y, Z) 是否可以传递一个代表 (X, Y, Z) 的变量 W,所以我可以使用 f(W) 而不是 f(X,Y, Z)? 如果是这样,如何做到这一点?
如果您的意思是传递一个元组或案例类而不是三个参数。那么是的,你可以。
首先,如果它是一个函数,你可以f.tupled(tuple): 如果它是一种方法,那么:(m _).tupled(tuple)。_(基本一样,只是用eta-expansion把方法变成了函数)。
f.tupled(tuple)
(m _).tupled(tuple)
如果你有一个案例类而不是一个元组,你可以将它的一个实例变成一个元组:(W.unapply(w).get 其中W是案例类的伴随对象,并且w是实例)。
W.unapply(w).get
W
w