|
From: Donal K. F. <don...@ma...> - 2008-06-15 14:46:32
|
Will Duquette wrote: > In Donal's example, we have an instance variable, utterance, declared > for the "animal" class; the variable is visible without declaration in > subclass "cat"'s methods, e.g., "speak2". Oops. I misread. The speak2 method wouldn't have worked as written without either: a) the cat class defining [variable utterance], or b) an explicit [my variable utterance]. > This strikes me as dangerous; a new instance variable added to an > ancestor class that has the same name as a local variable used in a > subclass method can break the subclass method. > > Donal, in your example, "dog" declares utterance explicitly; does that > make any difference? Given that method "cat"'s "speak2" can see > utterance, it would appear not. I made a mistake in reading Arnulf's extension of my example. :-) The only variables automatically brought into a method are those that are declared at the same level (i.e. in the same class/object) as the method. This should mean that if a superclass increases the set of variables it declares, there will only be a problem if the parent declares a variable that a subclass was previously declaring (and they're used inconsistently). But that case is one I'm explicitly not providing help for working around; sealing each variable to its class completely makes for much more difficult introspection, and I'll need some arm twisting to change my mind on that. Donal. |