I have several questions:
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. 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. 3. Is there a way to pass a method as a parameter to another method. Thanks. -- Delyan Kalchev _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
> 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 _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Delyan Kalchev
On 03.12.2008, at 10:23, Delyan Kalchev wrote:
> I have several questions: Hi! Welcome to a truly object-oriented system, where you might be in for some surprises :) > 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. In Squeak, nothing can change an object except the object itself. All that you can do from the outside is to send messages to the the object, which it can choose to react on or not. So design your objects carefully. > 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. When written like this, it cannot. Do you have a specific use case? I can't remember I ever needed this. > 3. Is there a way to pass a method as a parameter to another method. That would be a "block" which is pretty much an anonymous method written in-line: sortBlock := [:a :b | a sortsBefore: b]. myArray sortedBy: sortBlock. Everything in square brackets creates a "block" object, you can put any code inside it which is not evaluated immediately but later, for example inside the sortedBy: method: sortedBy: aBlock | x y | ... (aBlock value: x value: y) ifTrue: [...] ifFalse: [...] Normally you would not use a variable to hold the block, I just wrote it that way to make the meaning more obvious. So usually you'ld write myArray sortedBy: [:a :b | a sortsBefore: b]. Another, less flexible way is to just pass a method name (which we call a "selector"): myArray sortedUsing: #sortsBefore: which then needs to be "performed": sortedUsing: aSelector | x y | ... (x perform: aSelector with: y) ifTrue: [...] ifFalse: [...] And just to wrap that up, "passing a method" would literally be possible, too, since methods are of course objects on their own, but that's not what you meant. - Bert - _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Hello Bert and Lukas.
Thank you both for the helpful answers and thanks Bert for the warm welcome. About the first question: Actually I cannot think of a philosophical reason to limit the access to an object that the object itself permits. It seems natural with the encapsulation and now I feel the lack of necessity of explicitly marking an object as "const". I surely am fond of solving problems by the means of good design than putting extra complexity in the function call semantics. About the second question: I do not have any use case, as a matter of fact. This is just a product of my C++-affected thoughts. I believe this is another moment when good design must come in the front. I've thought about the block method, but the other method with the symbols is quite interesting, either. Thanks guys. I have other questions in mind and I don't feel shy of asking :). On Wed, Dec 3, 2008 at 2:02 PM, Bert Freudenberg <[hidden email]> wrote: > On 03.12.2008, at 10:23, Delyan Kalchev wrote: > >> I have several questions: > > Hi! Welcome to a truly object-oriented system, where you might be in for > some surprises :) > >> 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. > > In Squeak, nothing can change an object except the object itself. All that > you can do from the outside is to send messages to the the object, which it > can choose to react on or not. > > So design your objects carefully. > >> 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. > > When written like this, it cannot. > > Do you have a specific use case? I can't remember I ever needed this. > >> 3. Is there a way to pass a method as a parameter to another method. > > > That would be a "block" which is pretty much an anonymous method written > in-line: > > sortBlock := [:a :b | a sortsBefore: b]. > myArray sortedBy: sortBlock. > > Everything in square brackets creates a "block" object, you can put any code > inside it which is not evaluated immediately but later, for example inside > the sortedBy: method: > > sortedBy: aBlock > | x y | > ... > (aBlock value: x value: y) > ifTrue: [...] > ifFalse: [...] > > Normally you would not use a variable to hold the block, I just wrote it > that way to make the meaning more obvious. So usually you'ld write > > myArray sortedBy: [:a :b | a sortsBefore: b]. > > Another, less flexible way is to just pass a method name (which we call a > "selector"): > > myArray sortedUsing: #sortsBefore: > > which then needs to be "performed": > > sortedUsing: aSelector > | x y | > ... > (x perform: aSelector with: y) > ifTrue: [...] > ifFalse: [...] > > And just to wrap that up, "passing a method" would literally be possible, > too, since methods are of course objects on their own, but that's not what > you meant. > > - Bert - > > _______________________________________________ > Beginners mailing list > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners > -- Delyan Kalchev _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |