I tried to run the example of GNU Smalltalk in the documentation but ran into an issue.
Object subclass: Account [
| balance |
new [
| r |
r := super new.
r init.
^r
]
init [
'initialize account' printNl.
balance := 0
]
get [
^balance
]
]
In the new
method the init
message is never sent to the Account
method.
Heres my output:
st> Account new get
nil
st> Account new init get
'initialize account'
0
I took this example from the GNU Smalltalk Documentation.
Can somebody help me spot the error? I assumed that perhaps the init
method of super is called, but Object
does not have a init
method. Furthermore should super
create a instance of the current class?
Thanks Benjamin for the answer
So my problem was that i did not distinguish between class functions (new
) and Object functions
Fixed Code
Object subclass: Account [
| balance |
init [ balance := 0 ]
get [ ^balance ]
]
Account class extend [
new [ ^ (super new init) ]
]
st> Account new get
0