Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rochester!cornell!uw-beaver!tektronix!tekcrl!tekchips!jans From: jans@tekchips.TEK.COM (Jan Steinman) Newsgroups: comp.lang.smalltalk Subject: Re: literals Message-ID: <1448@tekchips.TEK.COM> Date: Mon, 13-Jul-87 20:36:15 EDT Article-I.D.: tekchips.1448 Posted: Mon Jul 13 20:36:15 1987 Date-Received: Wed, 15-Jul-87 02:14:50 EDT Organization: Tektronix Inc., Beaverton, Or. Lines: 47 johnson@uiucdcsp: >jan@tekchips says that if I want constants, I should make them by subclasses. >That is exactly my complaint. The flaw in Smalltalk is that I cannot. >So-called constant arrays are by default of class Array, which is not >constant. They should instead be in class ConstantArray, which has no >at:put: message. It will probably have a basicAt:put: message, but that >won't bother me. Again, the flaw is not in Smalltalk. Adding this code to your image will add a new class that exhibits array accessing behavior similar to that of Symbols. I don't know what "so-called constant arrays" are (literals?), but now you have "real" ConstantArrays! (I still maintain that there is no such thing as a constant in Smalltalk!) The compiler could be hacked to cause the "#()" notation to generate ConstantArrays, but that is left as an exercise for the reader. This took less than one minute to write in Smalltalk. After adding this code, try evaluating: c _ #('test' #foo 456 1.01) asConstantArray. c at: 2 put: #bar. -------------- ConstantArrayHack.st -------------------- Array variableSubclass: #ConstantArray instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Collections-Hacked'! ConstantArray comment: 'This class removes Array storage protocol for those who feel that Smalltalk constants are needed.'! !ConstantArray methodsFor: 'accessing'! at: index put: anObject "ConstantArrays do not allow modification of their contents." self error: self class name, 's cannot be modified!!'! ! !Array methodsFor: 'converting'! asConstantArray "Return an unmodifiable copy of the receiver." | constant | constant _ ConstantArray new: self size. 1 to: self size do: [:i| constant basicAt: i put: (self at: i)]. ^constant! !