<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">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.<div><br></div><div>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.</div><div><br></div><div>Thanks for noticing this.</div><div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><div>Kim</div><div><br></div></span><br class="Apple-interchange-newline">
</div>
<br><div><div>On Sep 23, 2014, at 9:01 PM, Andrew P. Black <<a href="mailto:black@cs.pdx.edu">black@cs.pdx.edu</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><meta http-equiv="Content-Type" content="text/html charset=windows-1252"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">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<div>constant or variable."</div><div><br></div><div>Program 1:</div><div><br></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>class test.with(x:Number) {</div><div> def x: Number = 2</div><div> print(x)</div><div>}</div><div><br></div></blockquote>What about the similar program<div><br></div><div>Program 2:<br><div><br></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>class test.with(x:Number) {</div><div> method x -> Number { 2 }</div><div> print(x)</div><div>}</div></blockquote><div><br></div><div>This is just as bad (in both programs there is no way to access the parameter <i>x</i>), 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.</div><div><br></div><div>I tried implementing a slightly stricter version of the no shadowing rule, in which it’s illegal to declare a method <i>y</i> inside the scope of a def of <i>y. </i> This makes the following program illegal:</div><div><br></div><div>Program 3:</div><div><br></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>class y.new { … }</div><div><br></div><div>class z.new {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>method y { … }</div><div>}</div></blockquote><div><br></div><div>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.</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Andrew</div><div> </div></div><div><br></div><div><br></div></div>_______________________________________________<br>Grace-core mailing list<br><a href="mailto:Grace-core@cecs.pdx.edu">Grace-core@cecs.pdx.edu</a><br>https://mailhost.cecs.pdx.edu/mailman/listinfo/grace-core<br></blockquote></div><br></div></body></html>