Scala:语法糖 [英] Scala : Syntactic Sugar

查看:92
本文介绍了Scala:语法糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力了解如何阅读此功能:

I have been scratching my head to understand how to read this function:

private def greeterBehavior(currentGreeting: String): Behavior[Command] =
    Actor.immutable[Command] { (ctx, msg) =>
      msg match {
        case WhoToGreet(who) =>
          greeterBehavior(s"hello, $who")
        case Greet =>
          println(currentGreeting)
          Actor.same
      }
    }

问题:

1)函数获取字符串并返回 Behavior [Command] 。 (理解)
但是.. Actor.immutable [Command] 是什么?
这是类型转换吗?还是物体?

1 ) function takes string and returns Behavior[Command] . ( understood) But .. what is Actor.immutable[Command] ? Is this a type casting ? or is it an object ?

2)如果我必须理解这样的语法,我可以参考的最佳地方或书是什么?

2) if I have to understand such syntax what is the best place or book I can refer?

推荐答案

不可变 Actor 的一种方法,该方法具有通用类型参数,在这种情况下,类型为 Command

immutable is a method on Actor, which takes in a generic type parameter, in this case that type is Command.

任何值得阅读的Scala入门资料都应涵盖泛型。 在Scala中编程和不耐烦的Scala都很受欢迎。

Any intro to Scala material worth reading should cover generics. "Programming in Scala" and "Scala for the Impatient" are both popular.

这篇关于Scala:语法糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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