Mushtaq Ahmed | 11 Oct 18:49

self type is not inherited


Given that,

trait A
trait B {this: A =>}

Each time we extend B, we need to explicitly specify the self type as A:

trait C extends B {this: A =>}
trait D extends B {this: A =>}
trait E extends B {this: A =>}

Can't this repetition be avoided? Shouldn't self type of C, D and E default
to A unless explicitly specified otherwise? Currently, this gives the
following error:

trait F extends B
error: illegal inheritance; self-type F does not conform to B's selftype B
with A

Regards,
Mushtaq
--

-- 
View this message in context: http://www.nabble.com/self-type-is-not-inherited-tp19934242p19934242.html
Sent from the Scala mailing list archive at Nabble.com.

gerry xiao | 14 Oct 11:27

Re: self type is not inherited

Trait B's self type is A, An Instances of B should also be instances of A,but C,D,E couldn't assure this, so you need redeclare it;
it seems self type can't be inherited


gerry


2008/10/12 Mushtaq Ahmed <mushtaq.a <at> gmail.com>

Given that,

trait A
trait B {this: A =>}

Each time we extend B, we need to explicitly specify the self type as A:

trait C extends B {this: A =>}
trait D extends B {this: A =>}
trait E extends B {this: A =>}

Can't this repetition be avoided? Shouldn't self type of C, D and E default
to A unless explicitly specified otherwise? Currently, this gives the
following error:

trait F extends B
error: illegal inheritance; self-type F does not conform to B's selftype B
with A

Regards,
Mushtaq
--
View this message in context: http://www.nabble.com/self-type-is-not-inherited-tp19934242p19934242.html
Sent from the Scala mailing list archive at Nabble.com.


Sean McDirmid | 14 Oct 11:52

Re: self type is not inherited

Easy, just avoid self types:

trait B {
  def self : A
  protected implicit def coerce(b : this.type) : A = self
}

They don't work on the outside anyways, so I always role my own. Just
remember to close the loop (self = this) when you create a concrete
sub-class.

Sean

On Sun, Oct 12, 2008 at 12:52 AM, Mushtaq Ahmed <mushtaq.a <at> gmail.com> wrote:
>
> Given that,
>
> trait A
> trait B {this: A =>}
>
> Each time we extend B, we need to explicitly specify the self type as A:
>
> trait C extends B {this: A =>}
> trait D extends B {this: A =>}
> trait E extends B {this: A =>}
>
> Can't this repetition be avoided? Shouldn't self type of C, D and E default
> to A unless explicitly specified otherwise? Currently, this gives the
> following error:
>
> trait F extends B
> error: illegal inheritance; self-type F does not conform to B's selftype B
> with A
>
> Regards,
> Mushtaq
> --
> View this message in context: http://www.nabble.com/self-type-is-not-inherited-tp19934242p19934242.html
> Sent from the Scala mailing list archive at Nabble.com.
>
>

Alex Boisvert | 14 Oct 20:26
Favicon

Re: self type is not inherited

I think we're drifting from the original question but if you're looking for a way to reduce boilerplate in the original code, it's easier to just extend the trait rather than using it as a self-type.

trait B extends A

Aren't self-types useful only if you have some kind of circular dependency between the types?

alex

On Tue, Oct 14, 2008 at 2:52 AM, Sean McDirmid <sean.mcdirmid <at> gmail.com> wrote:
Easy, just avoid self types:

trait B {
 def self : A
 protected implicit def coerce(b : this.type) : A = self
}

They don't work on the outside anyways, so I always role my own. Just
remember to close the loop (self = this) when you create a concrete
sub-class.

Sean




On Sun, Oct 12, 2008 at 12:52 AM, Mushtaq Ahmed <mushtaq.a <at> gmail.com> wrote:
>
> Given that,
>
> trait A
> trait B {this: A =>}
>
> Each time we extend B, we need to explicitly specify the self type as A:
>
> trait C extends B {this: A =>}
> trait D extends B {this: A =>}
> trait E extends B {this: A =>}
>
> Can't this repetition be avoided? Shouldn't self type of C, D and E default
> to A unless explicitly specified otherwise? Currently, this gives the
> following error:
>
> trait F extends B
> error: illegal inheritance; self-type F does not conform to B's selftype B
> with A
>
> Regards,
> Mushtaq
> --
> View this message in context: http://www.nabble.com/self-type-is-not-inherited-tp19934242p19934242.html
> Sent from the Scala mailing list archive at Nabble.com.
>
>

David MacIver | 14 Oct 22:17

Re: self type is not inherited

There are sometimes advantages to using the self type. e.g.
inheritance at that point can change the initialisation and override
orders.

On Tue, Oct 14, 2008 at 7:26 PM, Alex Boisvert <boisvert <at> intalio.com> wrote:
> I think we're drifting from the original question but if you're looking for
> a way to reduce boilerplate in the original code, it's easier to just extend
> the trait rather than using it as a self-type.
>
> trait B extends A
>
> Aren't self-types useful only if you have some kind of circular dependency
> between the types?
>
> alex
>
> On Tue, Oct 14, 2008 at 2:52 AM, Sean McDirmid <sean.mcdirmid <at> gmail.com>
> wrote:
>>
>> Easy, just avoid self types:
>>
>> trait B {
>>  def self : A
>>  protected implicit def coerce(b : this.type) : A = self
>> }
>>
>> They don't work on the outside anyways, so I always role my own. Just
>> remember to close the loop (self = this) when you create a concrete
>> sub-class.
>>
>> Sean
>>
>>
>>
>>
>> On Sun, Oct 12, 2008 at 12:52 AM, Mushtaq Ahmed <mushtaq.a <at> gmail.com>
>> wrote:
>> >
>> > Given that,
>> >
>> > trait A
>> > trait B {this: A =>}
>> >
>> > Each time we extend B, we need to explicitly specify the self type as A:
>> >
>> > trait C extends B {this: A =>}
>> > trait D extends B {this: A =>}
>> > trait E extends B {this: A =>}
>> >
>> > Can't this repetition be avoided? Shouldn't self type of C, D and E
>> > default
>> > to A unless explicitly specified otherwise? Currently, this gives the
>> > following error:
>> >
>> > trait F extends B
>> > error: illegal inheritance; self-type F does not conform to B's selftype
>> > B
>> > with A
>> >
>> > Regards,
>> > Mushtaq
>> > --
>> > View this message in context:
>> > http://www.nabble.com/self-type-is-not-inherited-tp19934242p19934242.html
>> > Sent from the Scala mailing list archive at Nabble.com.
>> >
>> >
>
>

Sean McDirmid | 15 Oct 04:16

Re: self type is not inherited

Another use case: self types are useful when inheritance isn't
possible, e.g., the virtual class pattern:

type T <: TImpl
trait TImpl { self : T =>
  ...
}

The limitations of self types are well known: they don't work outside
the instance (TImpl can't be automatically coerced to T via only a
self type) and they are very verbose (once you use the self type, you
have to re-affirm the annotation over and over again). On the other
hand, a self def (the alternative to a self type) can't act as a
stable identifier and doesn't allow for abstract override (because the
contract is informal).

On Wed, Oct 15, 2008 at 4:17 AM, David MacIver <david.maciver <at> gmail.com> wrote:
> There are sometimes advantages to using the self type. e.g.
> inheritance at that point can change the initialisation and override
> orders.


Gmane