简体   繁体   English

Scala-方法参数-def函数:行=>消息= {行=> {

[英]Scala - Method Parameters - def function: Row => Message = { row => {

Coming from Java background trying to understand this Scala code: 来自Java背景,试图了解以下Scala代码:

def function: Row => Message = {
  row => {
    // code
    // code
  }
}

As I understand we pass a function that returns type Message? 据我了解,我们传递了一个返回Message类型的函数? And then we actually implement row? 然后我们实际上实现了行? Why first Row is capital and second is not? 为什么第一行是资本,第二行不是资本?

Thanks. 谢谢。

Let's break it down. 让我们分解一下。

def function

Declare a method named function that has no inputs. 声明一个名为function的方法,该方法没有输入。

:Row => Message

The return type is a function that take a Row as input and returns a Message 返回类型是将Row作为输入并返回Message的函数

= row => {...}

Define and anonymous function with a single input named row. 使用名为row的单个输入定义和匿名函数。 This is the function that is returned (in Scala the last thing in a block is returned so you don't need to use the return keyword). 这是返回的函数(在Scala中,返回了块中的最后一件事,因此您无需使用return关键字)。 Scala is able to figure out what the input and output types of this function should be because they have to match the return type you declared for the method. Scala能够弄清楚该函数的输入和输出类型应该是什么,因为它们必须与您为该方法声明的返回类型匹配。

As I understand we pass a function that returns type Message? 据我了解,我们传递了一个返回Message类型的函数?

No. Nothing is being passed. 不,什么都没有通过。 The method function doesn't have any parameters, it doesn't take any arguments, so you can't pass any. 方法function没有任何参数,没有任何参数,因此您无法传递任何参数。

And then we actually implement row? 然后我们实际上实现了行?

No. Nothing is being implemented. 否。什么都没有执行。 There are no abstract members or abstract interfaces here to implement. 这里没有抽象成员或抽象接口要实现。

Why first Row is capital and second is not? 为什么第一行是资本,第二行不是资本?

It doesn't have to be. 不一定是。 It's just a convention. 这只是一个约定。 Types are usually written in PascalCase, parameters, fields, and methods in camelCase. 类型通常用PascalCase编写,参数,字段和方法用camelCase编写。 (Actually, it's the exact same thing in Java.) (实际上,在Java中是完全一样的。)

A rough Java equivalent would look something like this: 大致相当于Java的代码如下所示:

java.util.Function<Row, Message> function() {
  return row -> {
    // code
    // code
  };
}

As you can see, there really isn't much difference between the two. 如您所见,两者之间确实没有太大区别。

The return type is a function that takes type Row and returns Message . 返回类型是一个采用Row类型并返回Message的函数。 There should be some higher level implementation of that function. 该功能应该有一些更高层次的实现。

Check this link, it will help you understand: https://www.safaribooksonline.com/library/view/scala-cookbook/9781449340292/ch09s08.html 检查此链接,它将帮助您理解: https : //www.safaribooksonline.com/library/view/scala-cookbook/9781449340292/ch09s08.html

Not quite. 不完全的。

def means that you are defining a method. def表示您正在定义方法。 In this case it takes no argument and its return type is Row => Message . 在这种情况下,它不接受任何参数,其返回类型为Row => Message

So the function method you are defining returns a function that takes a Row and returns a Message . 因此,您正在定义的function方法将返回一个接受Row并返回Message的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM