self type is not inherited

View: New views
7 Messages — Rating Filter:   Alert me  

self type is not inherited

by Mushtaq Ahmed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: [scala] self type is not inherited

by gerry xiao :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>

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.



Re: [scala] self type is not inherited

by Sean McDirmid :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> 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.
>
>

Re: [scala] self type is not inherited

by Alex Boisvert-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> 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@...> 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.
>
>


Re: [scala] self type is not inherited

by David MacIver :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> 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@...>
> 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@...>
>> 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.
>> >
>> >
>
>

Re: [scala] self type is not inherited

by Sean McDirmid :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> wrote:
> There are sometimes advantages to using the self type. e.g.
> inheritance at that point can change the initialisation and override
> orders.

Re: [scala] self type is not inherited

by vpalle :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Could you elaborate on the "don't work on the outside" part?


Sean McDirmid 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@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.
>
>