scala中的self type
2024-08-29 17:16:42
scala目前的书籍有两《快学scala》和《scala编程》。资料确实不多,对这个语法使用只能结合使用进行理解。
先看源码:
private[spark] trait ActorLogReceive {
self: Actor => override def receive: Actor.Receive = new Actor.Receive { private val _receiveWithLogging = receiveWithLogging override def isDefinedAt(o: Any): Boolean = {
val handled = _receiveWithLogging.isDefinedAt(o)
if (!handled) {
log.debug(s"Received unexpected actor system event: $o")
}
handled
} override def apply(o: Any): Unit = {
if (log.isDebugEnabled) {
log.debug(s"[actor] received message $o from ${self.sender}")
}
val start = System.nanoTime
_receiveWithLogging.apply(o)
val timeTaken = (System.nanoTime - start).toDouble / 1000000
if (log.isDebugEnabled) {
log.debug(s"[actor] handled message ($timeTaken ms) $o from ${self.sender}")
}
}
} def receiveWithLogging: Actor.Receive protected def log: Logger
}
这块代码是spark的akka日志记录代码,很具有代表性,self:Actor =>这里有两个作用
1: ActorLogReceive的实现类必须继承Actor
2: 重写Actor里面的方法,该trait里面使用Actor里面的属性不要添加self.
本理实现一种代理,或是模板模式....
来看看他的使用
lazy val actorRef = actorSystem.actorOf(Props(new Actor with ActorLogReceive with Logging{
override def receiveWithLogging: Receive = ???
}))
最新文章
- 利用select实现IO多路复用TCP服务端
- Scrapy的中Css 选择器
- go语言包与包引用
- MYSQL学习笔记2--mysql 静态和动态plugin
- js操作iframe框架时应该屡清楚的一些概念
- 【原创】JPEG图像密写研究(二) 哈夫曼树的建立
- UML之部署图(Deployment Diagram)
- angular 实现自定义样式下拉菜单
- 【Sublime Text】sublime text 安装插件方法总结
- leetcode7:反转整数
- SpringCache学习实践
- nginx的http_sub_module模块使用之替换字符串
- 20165236 实验二 《Java面向对象程序设计》实验报告
- 关于final static修饰的常量部署后没有更新的问题
- 添加删除mysql用户
- SQL高效查询两个表不同的数据
- 解决appcompat中各种奇葩的错误
- 【jquery】图片前后对比效果——beforeAfter
- css 初始包含块
- CentOS普通用户没有sudo权限