穿过砖墙驱动单例类型
这是一个非常浓缩的版本:
case class Brickwall[A](otherSide: A)
trait Monoman { def me(m: this.type): Unit }
def test(m: Monoman): Unit = m.me(Brickwall(m).otherSide)
-> error: type mismatch;
found : Monoman
required: m.type
愚蠢的砖墙不让我通过。有什么想法可能吗?秘密的斯卡拉隧道效应?希望...
Here is a very condensed version:
case class Brickwall[A](otherSide: A)
trait Monoman { def me(m: this.type): Unit }
def test(m: Monoman): Unit = m.me(Brickwall(m).otherSide)
-> error: type mismatch;
found : Monoman
required: m.type
stupid brickwall doesn't let me through. any ideas how it might be possible? secret scala tunnel effects? hoping...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,Scala 编译器拒绝推断路径相关类型,因此一些类型注释会有所帮助:
As far as I know the Scala compiler refuses to infer path dependent types, so a little type annotation helps:
是的,Scala 编译器永远不会推断单例类型。
一种可能性是向 Monoman 特征添加工厂方法:
对于您的情况,这可能不是一个可行的解决方案。
Yes, singleton types are never inferred by the Scala compiler.
One possibility is to add a factory method to the Monoman trait:
Maybe that's not a viable solution in your case.
这是对工厂想法的尝试(我之前已经这样做过并放弃了,但是让我们再试一次):
所以虽然展开是透明的,但重新包装似乎不起作用,我怀疑这是不可能的让它工作,同样是因为
m.type
永远无法推断。here is a try with the factory idea (i had done this before and gave up, but well let's try again):
so while the unwrapping is transparent, the re-wrapping doesn't seem to be working, and i suspect it's not possible to get it working, again because
m.type
can never be inferred.