Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Smalltalk is a purely object-oriented language. You can only send messages to objects, which invokes a method defined on their class.

On which class did you define printSomething? If you defined it as an instance method, you need to invoke it on an instance of that class. E.g.:

MyClass new printSomething: 'Derpy'

If you defined it as a class method, you can send it directly to the class itself.

In which class did you define the method? You're not specifying to which class you're sending the message (telling it to execute the method).

In the case of Transcript show: 'Derp'., you're sending a message to the global variable Transcript (an instance of the Stream class), and show: is a class method implemented on Transcript or one of its superclasses.

If the method is defined on the same class that you're sending from, self is the keyword to use, so it would be self printSomething: 'Derpy'.

When trying to learn smalltalk, use a smalltalk environment. Don't use a command line interface, don't use an on-line web tool. Both are very useful, but not to learn smalltalk. They don't provide the feedback you need to learn smalltalk good and fast. If it doesn't allow you to write most of your code in the debugger, you won't learn smalltalk.

The book and environment developed to learn smalltalk is Pharo By Example. Use the image and vm from there. Pharo is developing fast, using a more recent version would be confusing.

In gnu-smalltalk 3.2.5.

Object subclass: Example [
    printSomething: what
    [
        10 timesRepeat: [
            Transcript show:what.
        ]
    ]
]

Eval [
    Transcript show: 'Derp'.
    (Example new) printSomething: 'Derpy'.
]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top