I think I figured it out. There is indeed "something deeper in Scala” :-)
abstract class A {
def a: this.type
}
class AA(i: Int) extends A {
def a = this
}
the above works ok. But if you return anything other than “this”, you will get
a compile error.
abstract class A {
def a: this.type
}
class AA(i: Int) extends A {
def a = new AA(1)
}
Error:(33, 11) type mismatch;
found : com.dataorchard.datagears.AA
required: AA.this.type
def a = new AA(1)
^
So you have to do:
abstract class A[T <: A[T]] {
def a: T
}
class AA(i: Int) extends A[AA] {
def a = new AA(1)
}
Mohit Jaggi
Founder,
Data Orchard LLC
www.dataorchardllc.com
> On Aug 30, 2016, at 9:51 PM, Mohit Jaggi <[email protected]> wrote:
>
> thanks Sean. I am cross posting on dev to see why the code was written that
> way. Perhaps, this.type doesn’t do what is needed.
>
> Mohit Jaggi
> Founder,
> Data Orchard LLC
> www.dataorchardllc.com <http://www.dataorchardllc.com/>
>
>
>
>
>> On Aug 30, 2016, at 2:08 PM, Sean Owen <[email protected]
>> <mailto:[email protected]>> wrote:
>>
>> I think it's imitating, for example, how Enum is delcared in Java:
>>
>> abstract class Enum<E extends Enum<E>>
>>
>> this is done so that Enum can refer to the actual type of the derived
>> enum class when declaring things like public final int compareTo(E o)
>> to implement Comparable<E>. The type is redundant in a sense, because
>> you effectively have MyEnum extending Enum<MyEnum>.
>>
>> Java allows this self-referential definition. However Scala has
>> "this.type" for this purpose and (unless I'm about to learn something
>> deeper about Scala) it would have been the better way to express this
>> so that Model methods can for example state that copy() returns a
>> Model of the same concrete type.
>>
>> I don't know if it can be changed now without breaking compatibility
>> but you're welcome to give it a shot with MiMa to see. It does
>> compile, using this.type.
>>
>>
>> On Tue, Aug 30, 2016 at 9:47 PM, Mohit Jaggi <[email protected]
>> <mailto:[email protected]>> wrote:
>>> Folks,
>>> I am having a bit of trouble understanding the following:
>>>
>>> abstract class Model[M <: Model[M]]
>>>
>>> Why is M <: Model[M]?
>>>
>>> Cheers,
>>> Mohit.
>>>
>