比赛中的明确回报 [英] Explicit return from play action

查看:105
本文介绍了比赛中的明确回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下操作

def login: Result = Action(parse.json) { request =>
  if (/* Let's we say here is some validation */) {
    return BadRequest("bad")
  }

  Ok("all ok")
}

这给我一个错误. Intellij建议使用Action[JsValue]类型.当我说return将是这种类型时,由于类型不匹配,我再次在BadRequest行出现错误.

This is giving me an Error. Intellij is suggesting Action[JsValue] type. When I say return will be of that type, I am getting again error on BadRequest line, because types doesn't match.

我尝试搜索此问题,并且发现了一些建议将Action[AnyContent]设置为返回类型的答案.但是我还是出错了.

I tried searching about this problem, and I found some answers which are suggesting to set Action[AnyContent] as return type. But I am STILL getting error.

我还需要从if返回...我不想在if之后写else,因为在某些更复杂的函数中,我很可能只有几个if语句应该中断动作,如果我使用if/else方法,代码将成为噩梦.

Also I need to return from if...I don't want to write else after that if, because in some more complex function most probably I will have few if statements which should break action, and if I use if/else approach, code will be nightmare.

推荐答案

当然可以.在Scala中,嵌套的匿名函数内部的"return"语句是通过抛出和捕获NonLocalReturnException来实现的.它在 Scala语言规范的第6.20节中说.

Of course it does. In Scala, a "return" statement inside a nested anonymous function is implemented by throwing and catching a NonLocalReturnException. It says so in the Scala Language Specification, section 6.20.

之所以这么做是因为出于理解,人们可以编写如下代码:

This is done because of for comprehensions, for people to be able to write code like this:

def loop() = {
  for (i <- 0 until 20) {
    if (someCondition) return
  }
}

该循环实际上等效于:

(0 until 20).foreach { i => if (someCondition) return }

您可以看到匿名功能吗?您能看到有问题的返回"不是指仅从该匿名函数返回吗?我认为这是一个设计错误.另一方面,在像Scala这样的语言中,无论如何都不需要返回".

Can you see the anonymous function? Can you see that the "return" in question doesn't refer to returning from that anonymous function alone? In my opinion this was a design mistake. On the other hand, in a language like Scala, "return" isn't needed anyway.

所以您在那里有一个匿名函数:

So you have an anonymous function in there:

Action(parse.json) { request => ... this one here ... }

在该函数内部,您使用的是返回",它在幕后触发了异常.

And inside that function you're using "return" which under the hood triggers an exception.

因此,Scala中的一般经验法则-永远不要使用返回.无论如何,这是一种面向表达的语言.您不需要它.

So the general rule of thumb in Scala - NEVER, EVER USE RETURN. It's an expression-oriented language anyway. You don't need it.

Action(parse.json) { request =>
  if (/* Let's we say here is some validation */) 
    BadRequest("bad")
  else
    Ok("all ok")
}

还有更多惯用语.顺便说一句-您也没有中断"或继续".适应没有他们的工作.另外,关于您的意见:

There, much more idiomatic. BTW - you also don't have "break" or "continue". Get used to working without them. Also, about your opinion:

我还需要从if ...中返回,我不想在if之后再写其他东西,因为在一些更复杂的函数中,最有可能的if语句应该中断动作,并且如果我使用if/否则,代码将成为噩梦.

Also I NEED to return from if...I don't want to write else after that if, because in some more complex function most probably I will have few if statements which should break action, and if I use if/else approach, code will be nightmare.

那是错误的,我真的讨厌使用依赖于返回,中断或继续使逻辑短路的代码,因为复杂的逻辑变得凌乱,我想清楚地了解一下:

That is wrong and I REALLY HATE working with code that relies on return, break or continue for short-circuiting the logic PRECISELY because complex logic gets messy and I want to have a clear view of:

  1. 不变式,如果我们谈论的是循环
  2. 退出条件
  3. 分支机构/路径

使用返回,中断或继续会破坏所有3点的清晰度.它们并不比GOTO跳跃好得多.而且,如果您具有复杂的功能,则在阅读开始成为问题时,可以将它们分解为多个功能.

Using return, break or continue destroys clarity on all 3 points. They aren't much better than GOTO jumps. And if you have complex functions, break them into multiple functions if reading it starts to become a problem.

此外,"match"语句比多个"if/else"分支好得多.一旦您习惯了它们,便会爱上它们,尤其是考虑到在某些情况下即使您缺少分支,编译器甚至可以保护您.

Also, much better than multiple "if/else" branches are "match" statements. Once you get used to them, you'll love them, especially given that the compiler can even protect you in some cases in which you're missing a branch.

这篇关于比赛中的明确回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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