3
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.

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • You have a capitalization error in your code. The method name has a capital P, but you're trying to call it with a lowercase p. – fzwo Jun 22 '13 at 21:05
  • @fzwo Nonetheless, the same error is produced even when the capitalization error has been corrected. I still haven't figured out the correct syntax for Smalltalk function calls. – Anderson Green Jun 22 '13 at 21:08
  • For next time, add a tag saying which Smalltalk you're using. GNU Smalltalk and Squeak/Pharo work fairly differently in terms of how they handle source code. – Frank Shearar Jun 26 '13 at 08:52

5 Answers5

2

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'.

fzwo
  • 9,842
  • 3
  • 37
  • 57
  • I did not define the method in a class: is it necessary for all methods in Smalltalk to be defined inside a class? – Anderson Green Jun 22 '13 at 21:14
  • Then where/how did you define it? I'm a little rusty, and different Smalltalk environments do certain things (like defining stuff in the workspace) differently, but generally, Smalltalk is very very object-oriented. – fzwo Jun 22 '13 at 21:19
  • 1
    You cannot define functions in Smalltalk, only methods. Also: Transcript is not a class but a global variable holding an instance of Stream class. – Johan B Jun 22 '13 at 21:23
  • @JohanB Thanks, I've edited my answer to be correct WRT Transcript. – fzwo Jun 22 '13 at 21:25
  • @AndersonGreen I have no idea how to create or edit classes in ideone, sorry. As Johan said, it is not possible in Smalltalk to define functions, so if you can't create or edit classes in your IDE, you cannot create methods. I guess in that case, ideone is not great for anything that goes beyond calling methods on pre-existing classes' instances (same goes for all other object-oriented languages as well, except that most of them aren't as strictly/cleanly object-oriented as Smalltalk, and do allow functions). – fzwo Jun 22 '13 at 21:30
  • @fzwo Should Smalltalk methods be defined inside the body of a class ([like this](http://pastebin.com/raw.php?i=DTzg8T8T)), or should they be defined elsewhere? I've been trying to call the method from this class, without any success so far. – Anderson Green Jun 22 '13 at 21:54
  • @AndersonGreen Smalltalk is usually very tightly integrated with its IDE. In the IDE, there is a thing called a class hierarchy browser (A tree of Classes), where you choose a Class, which has two lists of methods (class methods and instance methods) and lists of variables. Smalltalk does not use text files like other languages. Think of it more object oriented: Each class is an object (actually true in Smalltalk), which has its own properties (methods, etc.). So I can't answer your question. There is no syntax to tell "this method is part of this class", because the class is not a text file. – fzwo Jun 22 '13 at 22:25
  • Here is a nice image of how a typical class browser looks in a Smalltalk IDE: http://i.stack.imgur.com/xybBn.png Once you've worked with that, "classical", file-based IDEs will feel very 80s. I don't know why files are not abstracted away more by modern IDEs. – fzwo Jun 22 '13 at 22:27
  • @JohanB: `double := [:x | x * 2]` defines a function :) – Frank Shearar Jun 25 '13 at 18:08
  • @FrankShearar true. I should have written global functions, which is what the code excerpt seemed to indicate. I essentially wanted to point out that the printSomething that is noted in the example can only be defined as a method in a class. – Johan B Jun 25 '13 at 18:19
  • `Smalltalk at: #Double put: [:x | x * 2]` :) I'm just being pedantic. – Frank Shearar Jun 25 '13 at 21:26
2

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.

Johan B
  • 2,461
  • 14
  • 16
  • Maybe check out Pharo Smalltalk and the excellent online book: www.pharo-project.org. This might help you more, especially for learning Smalltalk in general. – Johan B Jun 22 '13 at 21:26
2

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.

Stephan Eggermont
  • 15,847
  • 1
  • 38
  • 65
2

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.

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
2

In gnu-smalltalk 3.2.5.

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

Eval [
    Transcript show: 'Derp'.
    (Example new) printSomething: 'Derpy'.
]
prosseek
  • 182,215
  • 215
  • 566
  • 871