how do I send the 2 messsages to the function
Hello, I have made this function : calculateNewFloor: aFloor index: index    "comment stating purpose of message"   |action floor |   action:= SantaAction getActionFor: aFloor. floor := action doMovementFor: aFloor now I want to send a message to this function : findBasement: input2    "solution to part1 of this challenge"     input2 withIndexDo: [ :element :index | |action| action:= SantaFloorAction getActionFor: element. floor := action calculateNewFloor: floor .. ].     ^ counter but how do I get the second argument in this Roelof
What you want to do is: findBasement: input2 "solution to part1 of this challenge" input2 withIndexDo: [ :element :index | |action| action:= SantaFloorAction getActionFor: element. floor := action calculateNewFloor: floor index: index ]. ^ counter Notice how I added the 'index: index' bit in the line just above the '^ counter', so you're actually performing: action calculateNewFloor: floor index: index If you had a method with three args you would declare it as: methodWithArg1: arg1 arg2: arg2 arg3: arg3 "assume this is in Foo class" self doSomething. and then you would call it in another place with ... "assume obj is some instance of Foo" obj methodWithArg1: 'some' arg2: 'arguments' arg3: #thatMightChange ... BTW the way you're framing the question makes me think you are not understanding how Smalltalk works. In Smalltalk (and by extension, in Pharo) you don't send messages to functions, you send messages to objects (all values that you can store in variables are objects in Smalltalk). In this context 'send message' is what in other languages is 'call the object's instance method with some arguments'. So you may be wondering: what's a 'message' anyway? A message is a method name (or more properly in the Smalltalk slang, its 'selector') plus some arguments. The idea is that what actually happens when a message is received by an object depends on the concrete implementation that that object has for that message; in other words, the method the object has; which may vary according the class of the object that received the message. Actually it's not unlike other object oriented languages, but the terminology and the syntax might be a little weird at first. Cheers, Diego -- Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
Op 25-11-2018 om 19:12 schreef dorellang:
What you want to do is:
findBasement: input2 "solution to part1 of this challenge"
input2 withIndexDo: [ :element :index | |action| action:= SantaFloorAction getActionFor: element. floor := action calculateNewFloor: floor index: index ]. ^ counter
Notice how I added the 'index: index' bit in the line just above the '^ counter', so you're actually performing:
action calculateNewFloor: floor index: index
If you had a method with three args you would declare it as:
methodWithArg1: arg1 arg2: arg2 arg3: arg3 "assume this is in Foo class" self doSomething.
and then you would call it in another place with
... "assume obj is some instance of Foo" obj methodWithArg1: 'some' arg2: 'arguments' arg3: #thatMightChange ...
BTW the way you're framing the question makes me think you are not understanding how Smalltalk works. In Smalltalk (and by extension, in Pharo) you don't send messages to functions, you send messages to objects (all values that you can store in variables are objects in Smalltalk). In this context 'send message' is what in other languages is 'call the object's instance method with some arguments'. So you may be wondering: what's a 'message' anyway? A message is a method name (or more properly in the Smalltalk slang, its 'selector') plus some arguments. The idea is that what actually happens when a message is received by an object depends on the concrete implementation that that object has for that message; in other words, the method the object has; which may vary according the class of the object that received the message.
Actually it's not unlike other object oriented languages, but the terminology and the syntax might be a little weird at first.
Cheers, Diego
-- Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
I know. Still trying to get a feeling about it and what takes me some time is that the use of if then is a bad smell I try to stretch my mind around that Roelof
Hi Roelof, is not easy to understand the question , without contents. #calculateNewFloor:index: - I assume you implemented the method on an action class (SantaAction). - In the method I see no usage from argument index but to send the method with a index: aFoor := aAction calculateNewFloor: aFloor index: anIndex. the method does not return a floor instance , as you expect in #findBasement: I hope you can solve you Santa puzzel, Nic
On 25 Nov 2018, at 17:42, Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I have made this function :
calculateNewFloor: aFloor index: index "comment stating purpose of message" |action floor | action:= SantaAction getActionFor: aFloor. floor := action doMovementFor: aFloor
now I want to send a message to this function :
findBasement: input2 "solution to part1 of this challenge"
input2 withIndexDo: [ :element :index | |action| action:= SantaFloorAction getActionFor: element. floor := action calculateNewFloor: floor .. ]. ^ counter
but how do I get the second argument in this
Roelof
participants (3)
-
dorellang -
Nicole de Graaf -
Roelof Wobben