Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to define and call functions in GNU Smalltalk?

Transcript show: 'Derp'.

printSomething: 'Derpy'.

"The method above produced this error:"
"prog.st:3: expected expression"

printSomething: what
    10 timesRepeat: [
        Transcript show:what.
        Transcript cr.
    ].

I'm trying to teach myself Smalltalk now, and I still haven't figured out how to call a function that I've written. I tried to call the function printSomething with the parameter 'Derpy' using the statement printSomething: 'Derpy'., but instead of calling the function, it produced the following error: prog.st:3: expected expression.

What am I doing wrong here, and what is the correct way to call functions with parameters in Smalltalk? None of the tutorials that I've read have answered my question so far, and I'm still a bit confused.

like image 925
Anderson Green Avatar asked Dec 28 '25 03:12

Anderson Green


1 Answers

I suspect that your errors are twofold:

Object class: #Example [
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Smalltalk Examples'
]

Example class extend [
    printSomething: what
        10 timesRepeat: [
            Transcript show:what.
        ]
]

Eval [
    Transcript show: 'Derp'.
    (Example new) printSomething: 'Derpy'.
]

Note the Eval [] block, and that you create an instance of Example, not NameOfSubclass.

like image 159
Frank Shearar Avatar answered Dec 31 '25 00:12

Frank Shearar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!