请参考对象定义中的对象类型 [英] refer to object type in object definition

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

问题描述




注意:第一个例子有效。它显示我想要得到的。第二个例子表明我想在没有样板的情况下声明它,但是没有这样做。






我只想简单的事情:

pre $ trait SelfTest [T<:SelfTest [T]] {_:T =>
def get:T = this
}
class Test扩展SelfTest [Test]
对象Test扩展测试

但没有样板类定义。例如:

  trait SelfTest [T<:SelfTest [T]] {_:T => 
def get:T = this
}
对象Test extends SelfTest [Test.type]

但这种方式被scala拒绝(涉及对象Test 的非法循环引用)。有没有像符号这样的魔术来表示创建的对象?

解决方案

这工作在scala 2.10 console ::

  scala>性状SelfTest [T <:SelfTest [_]] {_:T => def get:T = this} 
defined trait SelfTest

scala> class Test扩展SelfTest [测试]
定义的类测试

scala> case对象t扩展Test
定义模块t

scala> t.get
res1:Test = t



note: The first example works. It shows what I want to get. The second example shows that I'd like declare it without boilerplate but have failed to do so.


I just want to get simple thing:

trait SelfTest[T <: SelfTest[T]] {_ : T =>
  def get : T = this
}
class Test extends SelfTest[Test]
object Test extends Test

but without boilerplate class definition. Something like this:

trait SelfTest[T <: SelfTest[T]] {_ : T =>
  def get : T = this
}
object Test extends SelfTest[Test.type]

But this way is rejected by scala (illegal cyclic reference involving object Test). Is there any magic like # sign to denote created object?

解决方案

This worked in scala 2.10 console::

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

这篇关于请参考对象定义中的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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