Scala:在for循环中声明val,如果条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21407133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:01:48  来源:igfitidea点击:

Scala: declaring val within for loop, if condition

scala

提问by Lask3r

I'm a scala beginner and trying to understand how val works in Scala. I read that vals cannot be modified. When I do the following:

我是 Scala 初学者并试图了解 val 在 Scala 中的工作原理。我读到 vals 不能修改。当我执行以下操作时:

for( line <- Source.fromFile(args(0)).getLines() ) {
   val currentLine = line
   println(currentLine)
}

currentLine is updated in each iteration, while I expect it to be initialized with the first line and hold it till the end, or at least give a re-initialization error of some sort. Why is this so? Is the val created and destroyed in each iteration? My second question: I would like to use x outside if in the following code.

currentLine 在每次迭代中更新,而我希望它用第一行初始化并保持到最后,或者至少给出某种重新初始化错误。为什么会这样?val 是否在每次迭代中创建和销毁?我的第二个问题:如果在以下代码中,我想在外部使用 x 。

if( some condition is satisfied) val x = 2 else val x = 3

As of now, I'm getting an "Illegal start of simple expression" error. Is there a way to use x outside if?

到目前为止,我收到了“简单表达式的非法开始”错误。有没有办法在外面使用 x ?

回答by Kigyo

  1. Yes, the valis created and destroyed on each iteration.

  2. val x = if(condition) 2 else 3would do what you want.

  1. 是的,val每次迭代都会创建和销毁。

  2. val x = if(condition) 2 else 3会做你想做的。

Edit: You could rewrite 2. to if(conditon) {val x = 2} else {val x = 3}(to make it compile) but that would do nothing, since the if does not return anything and the variable can not be used outside the if

编辑:您可以将 2. 重写为if(conditon) {val x = 2} else {val x = 3}(以使其编译)但这将无济于事,因为 if 不返回任何内容并且该变量不能在外部使用if

回答by tuxdna

For Loop

For循环

You can break it down into a mapoperation.

您可以将其分解为一个map操作。

for( line <- Source.fromFile(args(0)).getLines() ) {
   val currentLine = line
   println(currentLine)
}

So this code transforms to

所以这段代码转换为

Source.fromFile(args(0)).getLines().map( line => block )

blockcould be any expression. Where in your case blockis:

block可以是任何表达。你的情况block是:

{
   val currentLine = line
   println(currentLine)
}

Here currentLineis local to blockand is created for each of the values of linegiven to mapoperation.

这里currentLine是本地的,block并且是为linemap操作的每个值创建的。

If-Else

如果别的

Again following is also wrong:

再次以下也是错误的:

if( some condition is satisfied) val x = 2 else val x = 3

Essentially if-elsein Scala returns a value. So it should be:

本质上if-else在 Scala 中返回一个值。所以应该是:

if( condition ) expression1 else expression1

In your case you it can be:

在您的情况下,您可以是:

if( condition ) { val x = 2 } else { val x = 3 }

However an assignment returns Unit( or voidif you want an analogy with Java / C++ ). So You can simply take the value of if-elselike so:

然而,一个赋值返回Unit(或者void如果你想要一个与 Java / C++ 的类比)。所以你可以简单地取这样的值if-else

val x = if( condition ) { 2 } else { 3 }
// OR
val x = if( condition ) 2 else 3

回答by benzonico

No answer mentioned it so in addition to what was said :

除了所说的内容之外,没有答案提到它:

  1. The valis made available for garbage collection on each iteration (and thus is not accessible from the next loop iteration). This is due to what is called scope of variableswhich is limited to the block in scala (same as Java).

  2. As stated by @Kigyo val x = if(condition) 2 else 3would do what you want, because you do only one assignation. If you put the assignation to val into the blocks, then the scope of this val is the block and thus not usable like you want to.

  1. val由可进行垃圾回收在每次迭代(因此是不是从下一个循环迭代访问)。这是由于调用的scope of variables内容仅限于 scala 中的块(与 Java 相同)。

  2. 正如@Kigyo 所说,你val x = if(condition) 2 else 3会做你想做的事,因为你只做一个分配。如果您将 val 的分配放入块中,则此 val 的范围是块,因此无法像您想要的那样使用。

回答by anonymous

1st question: yes, in every iteration a new val is created

第一个问题:是的,在每次迭代中都会创建一个新的 val

2nd question: you could rewrite it is

第二个问题:你可以重写它是

 val x =  if (some condition is satisfied)
  2
 else
  3