Refers to the type of an object in an object definition


Note: the first example works. This shows what I want to get. The second example shows that I would like to declare it without a template, but could not do it.


I just want to get a simple thing:

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

but without determining the class of the tile. Something like that:

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

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

+6
source share
1 answer

This worked in the 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 
0
source

Source: https://habr.com/ru/post/950489/


All Articles