“动作"一词是什么意思?使用Play框架在Scala函数定义中做什么? [英] What does the word "Action" do in a Scala function definition using the Play framework?

查看:76
本文介绍了“动作"一词是什么意思?使用Play框架在Scala函数定义中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Play应用程序,而我刚开始使用Scala.我看到在下面的函数中和花括号之前的等号后面有这个词Action.

I am developing Play application and I've just started with Scala. I see that there is this word Action after the equals sign in the function below and before curly brace.

def index = Action {
  Ok(views.html.index("Hi there"))
}

此代码有什么作用?我已经看到它与def index = {一起使用,但是没有与花括号前的单词一起使用.

What does this code do? I've seen it used with def index = { but not with the word before the curly brace.

我假设函数的名称是index.但是我不知道Action在这种情况下会做什么.

I would assume that the name of the function is index. But I do not know what the word Action does in this situation.

推荐答案

其他答案与您的具体情况有关.但是,您询问了一般情况,所以我将尝试从这种角度回答.

The other answers deal with your specific case. You asked about the general case, however, so I'll attempt to answer from that perspective.

首先,def用于定义方法

First off, def is used to define a method, not a function (better to learn that difference now). But, you're right, index is the name of that method.

现在,与您可能不熟悉的其他语言(例如C,Java)不同,Scala允许您使用表达式定义方法(如使用赋值运算符语法=所建议).也就是说,=之后的所有内容都是一个表达式,每次调用该方法时,该表达式都将被评估为 value .

Now, unlike other languages you might be familiar with (e.g., C, Java), Scala lets you define methods with an expression (as suggested by the use of the assignment operator syntax, =). That is, everything after the = is an expression that will be evaluated to a value each time the method is invoked.

因此,在Java中,您必须说:

So, whereas in Java you have to say:

public int three() { return 3; }

在Scala中,您只能说:

In Scala, you can just say:

def three = 3

当然,表达式通常更复杂(如您的情况).它可能是一个代码块,就像您更习惯看到的那样,在这种情况下,该值是该块中最后一个表达式的值:

Of course, the expression is usually more complicated (as in your case). It could be a block of code, like you're more used to seeing, in which case the value is that of the last expression in the block:

def three = {
   val a = 1
   val b = 2
   a + b
}

或者它可能涉及对其他对象的方法调用:

Or it might involve a method invocation on some other object:

def three = Numbers.add(1, 2)

实际上,后者确实是您特定示例中发生的事情,尽管需要更多说明才能理解原因.涉及两点魔术:

The latter is, in fact, exactly what's going on in your specific example, although it requires a bit more explanation to understand why. There are two bits of magic involved:

  1. 如果对象具有apply方法,则可以将其视为 function .例如,当您真正指的是Add.apply(1,2)时,您可以说Add(1, 2)(当然,假设有一个Add对象带有apply方法).只是要清楚一点,它不必是用object关键字定义的对象.任何具有适当apply方法的对象都可以.
  2. 如果某个方法具有单个 by-name 参数(例如def ifWaterBoiling(fn: => Tea)),则可以像ifWaterBoiling { makeTea }这样调用该方法.该块中的代码被延迟评估(并且可能根本不评估).这等同于编写ifWaterBoiling({ makeTea }). { makeTea }部分仅定义了一个表达式,该表达式未经参数cc传递给fn.
  1. If an object has an apply method, then you can treat the object as if it were a function. You can say, for example, Add(1, 2) when you really mean Add.apply(1,2) (assuming there's an Add object with an apply method, of course). And just to be clear, it doesn't have to be an object defined with the object keyword. Any object with a suitable apply method will do.
  2. If a method has a single by-name parameter (e.g., def ifWaterBoiling(fn: => Tea)), then you can invoke the method like ifWaterBoiling { makeTea }. The code in that block is evaluated lazily (and may not be evaluated at all). This would be equivalent to writing ifWaterBoiling({ makeTea }). The { makeTea } part just defines an expression that gets passed in, unevaluated, for the fn parameter.

这篇关于“动作"一词是什么意思?使用Play框架在Scala函数定义中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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