Unexpected MetaClass Behavior

Mark Guzdial guzdial at cc.gatech.edu
Wed Jul 12 13:39:21 UTC 2000


A couple of the students in our Summer Squeak class just found this behavior:
(1) File in the below
(2) Execute "TestObject new"
(3) When prompted, type in something like "alpha"
(4) Look in the class browser -- the name of the class TestObject has 
been replaced with the string that you entered!

Obviously, what the students were TRYING to do was to set an instance 
variable named "name."  What I expected the below code to do was to 
refuse to compile, saying that "name" wasn't a variable known to that 
method.  But it did compile, and it works! In a class message, the 
variable 'name' corresponds to the name of the class, and setting it 
changes the name of the class.

After looking at it for awhile, we figured it out, but it seems like 
an easy way to trip oneself up.  Perhaps "name" should be "className" 
or even "_name".

Or maybe this kind of dangerous metaprogramming should be at a different level?

Mark


---- Code Follows ----

Object subclass: #TestObject
	instanceVariableNames: 'instVarName1 instVarName2 '
	classVariableNames: 'ClassVarName1 ClassVarName2 '
	poolDictionaries: ''
	category: 'NameCrash'!

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

TestObject class
	instanceVariableNames: ''!

!TestObject class methodsFor: 'instance creation' stamp: 'kn 7/11/2000 14:18'!
new
	 name _ FillInTheBlank request:'Enter' initialAnswer:''.  ! !
--------------------------
Mark Guzdial : Georgia Tech : College of Computing : Atlanta, GA 30332-0280
Associate Professor - Learning Sciences & Technologies.
Collaborative Software Lab - http://coweb.cc.gatech.edu/csl/
(404) 894-5618 : Fax (404) 894-0673 : guzdial at cc.gatech.edu
http://www.cc.gatech.edu/gvu/people/Faculty/Mark.Guzdial.html





More information about the Squeak-dev mailing list