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

Should scalac give warnings or errors for identifiers in scope hidden by a supertype

2 replies
Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Hi,

abstract class Foo { val bar = 5 }

object Main { def main(args: Array[String]) = {
  val bar = 10
  new Foo { def spam = println(bar) }.spam
} }

In the above, it is the bar from Foo (5) that gets printed, because the supertype's members shadow the members from the enclosing scope.

My suggestion is that scalac begin to give warnings (or errors) for the *use* of identifiers for which the supertype's member takes precedence over the member from the enclosing scope.

I didn't report it as a bug/RFE, because it seems subjective.

Please, pick faults.

(It caused me some confusion when using specs.  The appropriate specs bug report: http://code.google.com/p/specs/issues/detail?id=47 )

Cheers,
Ricky.
Jesper Nordenberg
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Should scalac give warnings or errors for identifiers in sco

Ricky Clarkson wrote:
> Hi,
>
> abstract class Foo { val bar = 5 }
>
> object Main { def main(args: Array[String]) = {
> val bar = 10
> new Foo { def spam = println(bar) }.spam
> } }
>
> In the above, it is the bar from Foo (5) that gets printed, because the
> supertype's members shadow the members from the enclosing scope.
>
> My suggestion is that scalac begin to give warnings (or errors) for the
> *use* of identifiers for which the supertype's member takes precedence
> over the member from the enclosing scope.
>
> I didn't report it as a bug/RFE, because it seems subjective.

IMO, the problem is not primarily to figure out which method is called,
but rather that there is possibility for method "high jacking". The same
problem can be found in:

import A._
{
import B._
foo
}

Now if initially there is no B.foo, A.foo will be used, but if later foo
is added to B it will be used instead, thus it's "high jacked". I think
this might be cause enough to issue a warning when this type of "high
jacking" can occur. Note that an error will be issued for this code:

import A._
import B._
foo

/Jesper Nordenberg

malinova
Joined: 2010-11-01,
User offline. Last seen 1 year 48 weeks ago.
Это мы торчим н

Это мы торчим на этой жуткой планете месяцы и месяцы на неразведанной территории совсем одни, и неизвестно, какие опасности нас подстерегают Должны же мы хоть что-то получить взамен

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