New | BasicNew What is the Difference?

Ralph Johnson johnson at cs.uiuc.edu
Tue Jan 5 16:52:57 UTC 1999


	Behavior New
	Behavior BasicNew
	Behavior New:
	Behavior BasicNew:


First, they are "new", "basicNew", etc.  Capitalization is important.

Most programmers will never use basicNew and basicNew:.  new is used
for a normal class, while new: is used for a collection or some other
class with an indexible part.  Array new: 10 returns an array of length
10.  OrderedCollection new: 10 returns an OrderedCollection of length 0
but with space for 10 elements without growing.  In general, new returns
an object with every variable initialized to nil, unless you redefine
it, such as to initialize variables.  Only a few classes redefine new,
but it *does* get redefined, and that is why basicNew was defined.
Stream redefines new to generate an error message.  This prevents
people from getting an undefined Stream.  A subclass might define a
new class method "on:" to create instances.  This method will have
to call "new", but "new" was redefined by its subclass.  So, how can
it call "new"?  The secret is that Behavior defines basicNew to be
the same as new, and you are never supposed to redefine basicNew
(or any other methods with basic as a prefix).  So, if you need to
call the original version of "new", and "new" is redefined, call 
basicNew instead.

-Ralph Johnson





More information about the Squeak-dev mailing list