道具(新A与B)和道具[A与B]之间的区别 [英] Differences between Props(new A with B) and Props[A with B]

查看:103
本文介绍了道具(新A与B)和道具[A与B]之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用可堆叠特征模式创建一个简单的特征,以拦截来自任意参与者的消息。但是创建 Props 时,事实证明 Props(新的A和B)与<$ c $不同c>道具[A与B] 。即,第一个变体有效,而第二个变体无效。区别在哪里?

I was trying to create a simple trait to intercept received messages from arbitrary actors, using the stackable trait pattern. But when creating the Props, it turns out that Props(new A with B) is different from Props[A with B]. Namely, the first variant works but not the second one. Where is the difference?

一个最小的示例: https ://gist.github.com/ale64bit/df496ec2a43d0ec2ddb3

谢谢!

推荐答案

我不确定行为背后的确切原因,但是似乎使用参数化版本并不知道如何构造请求类型并简单地构造主要类型。即当您在actor中打印出实际的类时,您会得到

I'm not sure of the exact reason behind the behavior, but it would appear that using the parameterized version does not know how to construct the request type and simply constructs the primary type. I.e. when you print out the actual class inside the actor you get

Props(new FooActor with Interceptor) => Main$$anonfun$1$$anon$1
Props[FooActor with Interceptor] => FooActor

我最好的猜测是 ClassTag 或从 ClassTag 生成的生成器不理解带有Interceptor 语法的参数化 FooActor。但是,如果您创建了一个将这两个类混合使用的新类,它将起作用:

My best guess is that the ClassTag or builder generated from the ClassTag does not understand the parameterized FooActor with Interceptor syntax. If you however create a new class that mixes these two before, it does work:

class FooActorWithInterceptor extends FooActor with Interceptor

Props[FooActorWithInterceptor] => FooActorWithInterceptor

顺便说一句,我发现被咬了好多次后,应避免使用道具版本。尽管它适用于无参数构造函数版本,但您应该向构造函数添加一些参数 Props [A] 在编译时不会抱怨。同时 Props(new A)会给您一个编译错误。

As an aside, I've found from being bitten a number of times, that using the parametrized version of Props should be avoided. While it works for no-parameter constructor versions, should you ever add some parameters to the constructor Props[A] will not complain at compile time. Meanwhile Props(new A) will give you a compilation error.

这篇关于道具(新A与B)和道具[A与B]之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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