scala – 引用对象定义中的对象类型
发布时间:2020-12-16 18:46:51 所属栏目:安全 来源:网络整理
导读:注意:第一个示例有效.它显示了我想要的东西.第二个例子表明我想要在没有样板的情况下声明它但没有这样做. 我只想简单一点: trait SelfTest[T : SelfTest[T]] {_ : T = def get : T = this}class Test extends SelfTest[Test]object Test extends Test 但没
注意:第一个示例有效.它显示了我想要的东西.第二个例子表明我想要在没有样板的情况下声明它但没有这样做.
我只想简单一点: trait SelfTest[T <: SelfTest[T]] {_ : T => def get : T = this } class Test extends SelfTest[Test] object Test extends Test 但没有样板类定义.像这样的东西: trait SelfTest[T <: SelfTest[T]] {_ : T => def get : T = this } object Test extends SelfTest[Test.type] 但这种方式被scala拒绝(涉及对象Test的非法循环引用).是否有像#符号这样的魔法来表示创建的对象? 解决方法
这在scala 2.10控制台中有效::
scala> trait SelfTest[T<:SelfTest[_]] {_: T => def get:T = this } defined trait SelfTest scala> class Test extends SelfTest[Test] defined class Test scala> case object t extends Test defined module t scala> t.get res1: Test = t (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |