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:
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.
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?
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
"
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.
Post a Comment