Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何解释这个使用PartialFunction的def?_Scala - Fatal编程技术网

Scala 如何解释这个使用PartialFunction的def?

Scala 如何解释这个使用PartialFunction的def?,scala,Scala,我仍然习惯于Scala中部分函数的概念。 我遇到了以下我无法理解的陈述: 更新: def msgsToGet:部分功能[任何,单位] 在特征中定义如下: trait MyActorTrait { palindrome: String => def msgsToGet: PartialFunction[Any, Unit] //After the answers I received, I understand that the "def msgsToGet: //

我仍然习惯于Scala中部分函数的概念。 我遇到了以下我无法理解的陈述:

更新: def msgsToGet:部分功能[任何,单位] 在特征中定义如下:

trait MyActorTrait {
    palindrome: String => def msgsToGet: PartialFunction[Any, Unit]
    //After the answers I received, I understand that the "def msgsToGet:      //PartialFunction[Any, Unit]" represents the LHS of an abstract (as yet)     //unimplemented function (no function body yet)

}
那是什么意思? 我知道“def”表示一个函数。在本例中,它是一个名为
msgsToGet
的函数

还有一个冒号(:)。好的,直到现在,我一直认为“在冒号之后是类型,这就是我迷失的地方。”。 好的,这和:

def msgsToGet(): PartialFunction[Any, Unit]
[一种不接受参数并返回类型为
PartialFunction[Any,Unit]
的函数

PartialFunction[Any,Unit]
在我看来似乎是一种返回类型。但是没有函数体。请问这里发生了什么

这是一个较长但可读的语法糖吗


请帮我解释一下。

这和

def msgsToGet(): PartialFunction[Any, Unit]
Scala允许没有参数列表的方法(与一个空参数列表相反)


在字节码中,这两者是相同的,但在惯用的Scala中,对于无副作用的方法,尤其是访问器,没有参数列表是首选的。空参数列表是首选的,如果该方法有副作用的话。

def msgsToGet(): PartialFunction[Any, Unit]
Scala允许没有参数列表的方法(与一个空参数列表相反)


在字节码中,这两者是相同的,但在惯用的Scala中,对于无副作用的方法,尤其是访问器,没有参数列表是首选的。空参数列表是首选的,如果该方法有副作用。

如果没有函数体,则它是一个抽象方法。它是基于特征还是抽象类?抽象方法od和Java一样,表示实现类必须定义方法

您的分析是正确的:它是一个名为“msgsToGet”的函数,不接受任何参数,返回类型为
PartialFunction[Any,Unit]
的对象

下面是一个例子:

trait SomeTrait {
  def msgsToGet: PartialFunction[Any, Unit]
}

class SomeClass extends SomeTrait {
  def msgsToGet: PartialFunction[Any, Unit] = { 
    case x => println(x)      
  } // a case block is a partial function, so we can return this block from the function
}

val c = new SomeClass
val f = c.msgsToGet    // takes no parameters, returns a partial function
f("hey")               // we can call the partial function, which takes an Any parameter
// prints "hey"

如果没有函数体,那么它就是一个抽象方法。它是在trait上还是在抽象类上?一个抽象方法,比如Java,说实现类必须定义这个方法

您的分析是正确的:它是一个名为“msgsToGet”的函数,不接受任何参数,返回类型为
PartialFunction[Any,Unit]
的对象

下面是一个例子:

trait SomeTrait {
  def msgsToGet: PartialFunction[Any, Unit]
}

class SomeClass extends SomeTrait {
  def msgsToGet: PartialFunction[Any, Unit] = { 
    case x => println(x)      
  } // a case block is a partial function, so we can return this block from the function
}

val c = new SomeClass
val f = c.msgsToGet    // takes no parameters, returns a partial function
f("hey")               // we can call the partial function, which takes an Any parameter
// prints "hey"

只是另一个天真的问题:函数如何返回PartialFunction[Any,Unit]类型的值如果没有函数体?哦,那只是意味着它是一个抽象方法。Scala没有抽象关键字。如果没有函数体,def/var/val是抽象的。你直接回答了我的问题。@dhg的答案和你的答案都非常好,让人大开眼界。嗯..我将把你的问题标记为正确答案t另一个天真的问题:函数如何返回PartialFunction[Any,Unit]类型的值如果没有函数体?哦,那只是意味着它是一个抽象方法。Scala没有抽象关键字。如果没有函数体,def/var/val是抽象的。你直接回答了我的问题。@dhg的答案和你的答案都非常好,让人大开眼界。嗯..我将把你的问题标记为正确答案好答案。我将选择你的答案作为正确答案。非常好的答案。我承诺一旦我能够这样做,我将投票支持你的答案。直到现在,我还不知道案例块是一个部分函数。非常好的答案。我将选择你的答案作为正确答案。非常好的答案。我承诺一旦我能够这样做,我将投票支持你的答案。直到现在,我才知道case块是部分函数。