This page is no longer maintained — Please continue to the home page at www.scala-lang.org

overriding objects

3 replies
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

That wasn't bad:

https://issues.scala-lang.org/browse/SI-4972
https://github.com/paulp/scala-dev/tree/override-objects

Still needs some detail work.

The interesting bit which came up is what type the overriding object must have to be a valid override, and how to usefully report failure in that regard. As far as my little brain will take me, the type is the class bound (the least proper supertype of the type of of the overridden module.) So that's what I check against and that's what I report. Any thoughts.

override2.scala:16: error: overriding object Bar in trait Foo of type object Foo2.this.Bar;
object Bar has incompatible type
Note that Bar must conform to the overridden object's class bound.
found: Bippy with ScalaObject
req: Bippy with Bippo with ScalaObject
override object Bar extends Bippy {
^
one error found

// source which goes with the above
class Bippy {
def f = 1
}
trait Bippo

trait Foo {
object Bar extends Bippy with Bippo {
override def f = 2
}

def f(x: Bippo)
def g = f(Bar)
}

trait Foo2 extends Foo {
override object Bar extends Bippy {
override def f = 3
}
}

Martin Odersky
Joined: 2009-10-07,
User offline. Last seen 42 years 45 weeks ago.
Re: overriding objects

Sent from my phone

On Sep 8, 2011, at 8:03, Paul Phillips wrote:

> That wasn't bad:
>
> https://issues.scala-lang.org/browse/SI-4972
> https://github.com/paulp/scala-dev/tree/override-objects
>
> Still needs some detail work.
>
> The interesting bit which came up is what type the overriding object
> must have to be a valid override, and how to usefully report failure
> in that regard. As far as my little brain will take me, the type is
> the class bound (the least proper supertype of the type of of the
> overridden module.) So that's what I check against and that's what I
> report. Any thoughts.

That's indeed an interesting question. I believe class bound is right.

Martin

> override2.scala:16: error: overriding object Bar in trait Foo of
> type object Foo2.this.Bar;
> object Bar has incompatible type
> Note that Bar must conform to the overridden object's class bound.
> found: Bippy with ScalaObject
> req: Bippy with Bippo with ScalaObject
> override object Bar extends Bippy {
> ^
> one error found
>
> // source which goes with the above
> class Bippy {
> def f = 1
> }
> trait Bippo
>
> trait Foo {
> object Bar extends Bippy with Bippo {
> override def f = 2
> }
>
> def f(x: Bippo)
> def g = f(Bar)
> }
>
> trait Foo2 extends Foo {
> override object Bar extends Bippy {
> override def f = 3
> }
> }

adriaanm
Joined: 2010-02-08,
User offline. Last seen 31 weeks 4 days ago.
Re: overriding objects
looks good, but I think you need to pay the AsSeenFrom tax here as well:

let us consider (he declared solemnly)
class Bar[T]
class Foo[T] {   object A extends Bar[T]}
class Baz[S] extends Foo[S] {  override object A extends Bar[S]}
the compiler complains thusly
error: overriding object A in class Foo of type object Baz.this.A; object A has incompatible typeoverriding object A in class Foo of type object Baz.this.A with object A in class Baz of type object Baz.this.A; Note that A must conform to the overridden object's class bound.  found: Bar[S] with ScalaObject    req: Bar[T] with ScalaObject  override object A extends Bar[S]                  ^ one error found
if one pays one's dues:-              val cb1 = tp1.typeSymbol.classBound-              val cb2 = tp2.typeSymbol.classBound+              val cb1 = tp1.typeSymbol.classBound.asSeenFrom(self, tp1.typeSymbol.owner) +              val cb2 = tp2.typeSymbol.classBound.asSeenFrom(self, tp2.typeSymbol.owner)

it remains silent
cheersadriaan
ps: I created a pull request: https://github.com/paulp/scala-dev/pull/1

On Thu, Sep 8, 2011 at 9:53 AM, Martin Odersky <odersky@gmail.com> wrote:


Sent from my phone


On Sep 8, 2011, at 8:03, Paul Phillips <paulp@improving.org> wrote:

That wasn't bad:

 https://issues.scala-lang.org/browse/SI-4972
 https://github.com/paulp/scala-dev/tree/override-objects

Still needs some detail work.

The interesting bit which came up is what type the overriding object must have to be a valid override, and how to usefully report failure in that regard.  As far as my little brain will take me, the type is the class bound (the least proper supertype of the type of of the overridden module.) So that's what I check against and that's what I report.  Any thoughts.

That's indeed an interesting question. I believe class bound is right.

 Martin

override2.scala:16: error: overriding object Bar in trait Foo of type object Foo2.this.Bar;
object Bar has incompatible type
Note that Bar must conform to the overridden object's class bound.
 found: Bippy with ScalaObject
  req: Bippy with Bippo with ScalaObject
 override object Bar extends Bippy {
                ^
one error found

// source which goes with the above
class Bippy {
 def f = 1
}
trait Bippo

trait Foo {
 object Bar extends Bippy with Bippo {
  override def f = 2
 }

 def f(x: Bippo)
 def g = f(Bar)
}

trait Foo2 extends Foo {
 override object Bar extends Bippy {
  override def f = 3
 }
}

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: overriding objects

On 9/8/11 1:24 AM, Adriaan Moors wrote:
> looks good, but I think you need to pay the AsSeenFrom tax here as well:

I saw mr. parameterized types coming this way, but as I always struggle
with the precise formulation that is extremely helpful. And a pull
request, and you fixed the borked error message? I saw the error message
got mashed and fixed it by hand for the email, saying to myself "as if
anyone will actually look at the code between now and tomorrow morning."
I have learned an important lesson about not listening to cynical people
who talk to themselves.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland