[Newbies] About methods and parameters

Lukas Renggli renggli at gmail.com
Wed Dec 3 11:17:27 UTC 2008


> 1. As I see parameter passing in Smalltalk is always
> call-by-reference, so within a method it is possible to do any
> "destructive" actions over the passed object. Is there a way to
> pass/accept a parameter as "const" so that it won't be changed in the
> method.

No, not out of the box.

> 2. When we pass an object to a method, it can do anything with it, but
> is there a way to pass a variabe by reference and do change the passed
> variable. For example:
>
> i := 5.
> MyObj method: i.
>
> How can i be 6 after the method call.

Use a holder object:

    i := ValueHolder new.
    i contents: 5.
    MyObj method: i.
    i contents

> 3. Is there a way to pass a method as a parameter to another method.

Wrap the method call into a block:

    MyObj foo: [ Transcript show: 'hello' ]

Cheers,
Lukas

-- 
Lukas Renggli
http://www.lukas-renggli.ch


More information about the Beginners mailing list