【发布时间】:2011-10-02 07:01:28
【问题描述】:
在python中我可以做到
>>> (a,b) = (1,2)
>>> (b,a) = (a,b)
>>> (a,b)
(2, 1)
但在 scala 中:
Welcome to Scala version 2.8.1.final (OpenJDK Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> var (a,b) = (1,2)
a: Int = 1
b: Int = 2
scala> (a,b)=(b,a)
<console>:1: error: ';' expected but '=' found.
(a,b)=(b,a)
^
所以虽然我可以将 vars 初始化为一个元组,但我不能将它们分配为一个元组。除了使用 tmp var,还有什么办法可以解决这个问题?
【问题讨论】:
标签: scala functional-programming