[Grace-core] The no shadowing rule

Kim Bruce kim at cs.pomona.edu
Tue Sep 23 21:41:57 PDT 2014


The difference between the two is that in programs 1 & 2 the x which is a parameter is in the same scope as the def x (respectively, method x) inside the class.  In program 3, the class y is in an outer scope.

Nevertheless, I am troubled by the fact that the class y is in scope at the same time as method y.  While I would be disappointed to give up the use of color as a class and as a method of graphic objects, I would be willing to do it (starting after the current semester is over!) if it helps us come up with a consistent rule.  I'd suggest that for now, we declare it illegal to give two introductions of the same identifier in the same scope (ruling out examples 1 and 2), while emitting a warning for program 3.  After the semester is over, I could change the name of the color class to something like mkColor, but I'd rather not change names in the middle of the semester.

Thanks for noticing this.

Kim



On Sep 23, 2014, at 9:01 PM, Andrew P. Black <black at cs.pdx.edu> wrote:

> 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
>   
> 
> 
> _______________________________________________
> Grace-core mailing list
> Grace-core at cecs.pdx.edu
> https://mailhost.cecs.pdx.edu/mailman/listinfo/grace-core

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


More information about the Grace-core mailing list