Scala while循环一直返回Unit [英] Scala while loop returns Unit all the time

查看:26
本文介绍了Scala while循环一直返回Unit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,但我无法让它工作.只要我在案例中放置一个 while 循环,它就会返回一个单位,无论我在括号内进行什么更改.

I have the following code, but I can't get it to work. As soon as I place a while loop inside the case, it's returning a unit, no matter what I change within the brackets.

case While(c, body) =>
    while (true) {
      eval(Num(1))
    }
}

如何让这个 while 循环返回一个非 Unit 类型?

How can I make this while loop return a non-Unit type?

我尝试了在我的 while 条件周围添加括号,但它仍然没有达到预期的效果

I tried adding brackets around my while condition, but still it doesn't do what it's supposed to.

有什么指点吗?

更新

多一点背景信息,因为我没有真正解释代码应该做什么,如果我想得到一些帮助,这似乎很方便;

A little more background information since I didn't really explain what the code should do, which seems to be handy if I want to receive some help;

我已经定义了一个 eval(exp : Exp).这将评估一个函数.Exp 是一个抽象类.扩展了几个类,如 PlusMinus(一些更基本的操作)和一个 IfThenElse(cond : Exp, then : Exp, else : Exp).最后但并非最不重要的是,还有 While(cond: Exp, body: Exp).

I have defined a eval(exp : Exp). This will evaluate a function. Exp is an abstract class. Extended by several classes like Plus, Minus (few more basic operations) and a IfThenElse(cond : Exp, then : Exp, else : Exp). Last but not least, there's the While(cond: Exp, body: Exp).

应该如何使用它的示例;

Example of how it should be used;

eval(Plus(Num(1),Num(4)) 将导致 NumValue(5).(Num(v : Value) 的评估结果为NumValue(v).NumValue 继承了另一个抽象类 Value.

eval(Plus(Num(1),Num(4)) would result in NumValue(5). (Evaluation of Num(v : Value) results in NumValue(v). NumValue extends Value, which is another abstract class).

eval(While(Lt(Num(1),Var("n")), Plus(Num(1), Var("n"))))

Lt(a : Exp, b : Exp) 返回 NumValue(1) 如果 a

Lt(a : Exp, b : Exp) returns NumValue(1) if a < b.

推荐答案

从另一个答案中可以清楚地看出,Scala while 循环总是返回 Unit.Scala 的优点在于,如果它不能满足您的要求,您可以随时扩展它.

It's probably clear from the other answer that Scala while loops always return Unit. What's nice about Scala is that if it doesn't do what you want, you can always extend it.

这是一个类似于 while 的结构的定义,它返回最后一次迭代的结果(如果从未进入循环,它将抛出异常):

Here is the definition of a while-like construct that returns the result of the last iteration (it will throw an exception if the loop is never entered):

def whiley[T](cond : =>Boolean)(body : =>T) : T = {
  @scala.annotation.tailrec
  def loop(previous : T) : T = if(cond) loop(body) else previous
  if(cond) loop(body) else throw new Exception("Loop must be entered at least once.")
}

...然后您可以将其用作 while.(事实上​​,@tailrec 注释将使它编译成与 while 循环完全相同的东西.)

...and you can then use it as a while. (In fact, the @tailrec annotation will make it compile into the exact same thing as a while loop.)

var x = 10
val atExit = whiley(x > 0) {
  val squared = x * x
  println(x)
  x -= 1
  squared
}
println("The last time x was printed, its square was : " + atExit)

(请注意,我并不是声称该构造很有用.)

(Note that I'm not claiming the construct is useful.)

这篇关于Scala while循环一直返回Unit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆