[Grace-core] The no shadowing rule

Andrew P. Black black at cs.pdx.edu
Tue Sep 23 21:01:31 PDT 2014


Kim recently pointed out that the following program causes a compiler crash, but should be illegal, because it violates the no-shadowing rule, which states "It is an error to declare a constant or variable that shadows a lexically-enclosing
constant or variable."

Program 1:

class test.with(x:Number) {
      def x: Number = 2
      print(x)
}

What about the similar program

Program 2:

class test.with(x:Number) {
      method x -> Number { 2 }
      print(x)
}

This is just as bad (in both programs there is no way to access the parameter x), but it does not violate the rule.   In fact, with the addition of and “is public” annotation to the def of x in the first program, they are pretty much identical.  Which was why program 1 was getting through the existing checks: they treated defs and vars inside classes and objects as equivalent to methods.

I tried implementing a slightly stricter version of the no shadowing rule, in which it’s illegal to declare a method y inside the scope of a def of y.   This makes the following program illegal:

Program 3:

class y.new { … }

class z.new {
	method y { … }
}

Do we want program 3 to be legal?   If you say no, this will break objectdraw, where y = color and z = drawable.   If you say yes, we need a no shadowing rule that allows program 3 but disallows program 2.

	Andrew
  


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailhost.cecs.pdx.edu/pipermail/grace-core/attachments/20140923/69749b40/attachment.html>


More information about the Grace-core mailing list