【问题标题】:What does @ mean in Scala? [duplicate]Scala中的@是什么意思? [复制]
【发布时间】:2017-04-05 10:28:37
【问题描述】:

在研究akka 的来源时,我在akka.event.slf4j.SLF4JLogger 演员中发现了以下内容:

def receive = {
    //...
    case event @ Warning(logSource, logClass, message) ⇒
      withMdc(logSource, event) { Logger(logClass, logSource).warn("{}", message.asInstanceOf[AnyRef]) }

    case event @ Info(logSource, logClass, message) ⇒
      withMdc(logSource, event) { Logger(logClass, logSource).info("{}", message.asInstanceOf[AnyRef]) }

    case event @ Debug(logSource, logClass, message) ⇒
      withMdc(logSource, event) { Logger(logClass, logSource).debug("{}", message.asInstanceOf[AnyRef]) }
    //...
}

我不太明白@ 符号是什么。它不是一种方法,并且在范围内没有 event 的声明。 WarningInfoDebug 都是具有 apply 方法的对象。

【问题讨论】:

  • @c1moore 哦,对了,没找到相关答案

标签: scala pattern-matching partialfunction


【解决方案1】:

这叫做变量绑定:

除了独立变量模式,您还可以添加一个 可变为任何其他模式。您只需编写变量名,一个 at 符号 (@),然后是模式。这为您提供了变量绑定 图案。这种模式的意义是进行模式匹配 像往常一样,如果模式成功,将变量设置为 匹配的对象就像一个简单的变量模式。

http://www.artima.com/pins1ed/case-classes-and-pattern-matching.html

【讨论】:

    猜你喜欢
    • 2015-04-23
    • 2012-02-07
    • 2016-10-25
    • 1970-01-01
    • 2021-11-07
    • 2023-03-27
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多