Smalltalk referring to classes that are not yet defined

Viewed 55

I want to be able to write code like this:

MyObject subclass: #A instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CAT1'.
A subclass: #B instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CAT1'.

without running each line seperately.

When I try to do this I get an error saying that A isn't declared, even if it will be declared by the time the second line will be reached.

Is there a way to overcome this?

2 Answers

For deferred lookups of classes, you have to look up the class in the dictionary of globals, called Smalltalk (try to inspect and browse it). You can use accessors like classNamed: or at: to look up a name that might not be defined yet when the whole expression that you want to evaluate is compiled.

MyObject subclass: #A instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CAT1'.
(Smalltalk classNamed: #A) subclass: #B instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CAT1'.

Some dialects support the asClass message on Symbol, allowing you to do:

MyObject subclass: #A instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CAT1'.
#A asClasss subclass: #B instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CAT1
Related