"I have a mind like a steel... uh... thingy." Patrick Logan's weblog.

Search This Blog

Loading...

Tuesday, August 10, 2004

Eschew Special Case Languages

Another example of languages with special cases causing conflicts.

Another reason "constructors" should just be a regular message sent to a regular object. You already understand the simple, regular rules...

In the Smalltalk language, everything is an object. This includes not only numbers and all data structures, but even classes, methods, pieces of code within a method (blocks or closures), stack frames (contexts), etc. Even if and while structures are implemented as methods sent to particular objects.

4 comments:

Ian Bicking said...

An interesting contrast is Javascript, where "new" is a keyword, yet the language seems somewhat orthogonal despite that. Perhaps because "class" is not a keyword, and there isn't any class declaration syntax, nor any class objects (being prototype-based). It seems strangely backward.

Charles Miller said...

Could you explain in more detail why Smalltalk is immune to this problem? What happens in Smalltalk if a superclass initialiser calls a method on 'self' that is overridden in the subclass?

Anonymous said...

B subclass: #D
instanceVariableNames: 'value' !

!D class methodsFor: 'instance creation'!
^super new initialize ! !

!D methodsFor: 'initialize-release'!
initialize
value := 42 !

method1
Transcript show:
(value = 42
ifTrue: ['value = 42, all is good']
ifFalse: ['value ~ 42, what is wrong']
); cr ! !


Object subclass: #B !

!B class methodsFor: 'instance creation'!
new
^super new method1 ! !

!B methodsFor: 'initialize-release'!
method1
! !


"D new
value ~ 42, what is wrong
"

Patrick Logan said...

What's different for Smalltalk is that you don't think of constructors or initializers any differently than you think of any other message that can be sent to an object.

Can you make a mistake? Yes. But not because there are obscure rules for special cases.

Blog Archive

About Me

Portland, Oregon, United States
I'm usually writing from my favorite location on the planet, the pacific northwest of the u.s. I write for myself only and unless otherwise specified my posts here should not be taken as representing an official position of my employer. Contact me at my gee mail account, username patrickdlogan.