Hi, I wanted to write a method that: - if (a = b) checks other conditions that are specified in the ifTrue block and return this (boolean) value - returns false if (a!=b) So I wrote: myMethod ^ (a=b) ifTrue: [ aBlockCheckingConditions]. However, when (a!=b) the method returns nil (since false ifTrue: [] returns nil). So do you have an idea how I can do to express that or should I mandatorily wrote also an ifFalse:? The problem comes certainly because we donât know that the ifTrueBlock will return a boolean. If you have prettier ideas, I am interested in. Anne
On 02 Apr 2015, at 17:15, Anne Etien <anne.etien@univ-lille1.fr> wrote:
Hi,
I wanted to write a method that: - if (a = b) checks other conditions that are specified in the ifTrue block and return this (boolean) value - returns false if (a!=b)
So I wrote: myMethod ^ (a=b) ifTrue: [ aBlockCheckingConditions].
However, when (a!=b) the method returns nil (since false ifTrue: [] returns nil). So do you have an idea how I can do to express that or should I mandatorily wrote also an ifFalse:?
I always use #ifTrue:ifFalse: in such situations, just to be explicit. It makes reading the code easier too.
The problem comes certainly because we donât know that the ifTrueBlock will return a boolean.
Thereâs been years of debate on this :)
If you have prettier ideas, I am interested in.
Anne
Cheers, Max
On 02 Apr 2015, at 17:31, Peter Uhnák <i.uhnak@gmail.com> wrote:
^ (a=b) ifTrue: [ aBlockCheckingConditions].
Boolean is still just an object, so you can use a cascade ^ (a=b) ifTrue: [ aBlockCheckingConditions]; yourself
But that will always return the the boolean from the first statement! If you want to return something from the conditions block you canât do that. An alternative might be ^ a = b and: [ condition ]
Peter
Le 2 avr. 2015 à 18:02, Max Leske <maxleske@gmail.com> a écrit :
On 02 Apr 2015, at 17:31, Peter Uhnák <i.uhnak@gmail.com> wrote:
^ (a=b) ifTrue: [ aBlockCheckingConditions].
Boolean is still just an object, so you can use a cascade ^ (a=b) ifTrue: [ aBlockCheckingConditions]; yourself
But that will always return the the boolean from the first statement! If you want to return something from the conditions block you canât do that.
An alternative might be
^ a = b and: [ condition ]
Yes!! Good idea. Thanks. Anne
Peter
Basically ifTrue: aBlock is equivalent to: ifTrue: aBlock ifFalse: [ ] This is why ifTrue: answers nil if the receiver is false, because the evaluation of an empty block answers nil ( [ ] is the same thing as [ nil ] ). So you have to write it explicitly. The #and: version is probably the best :-). 2015-04-02 9:12 GMT-07:00 Anne Etien <anne.etien@univ-lille1.fr>:
Le 2 avr. 2015 à 18:02, Max Leske <maxleske@gmail.com> a écrit :
On 02 Apr 2015, at 17:31, Peter Uhnák <i.uhnak@gmail.com> wrote:
^ (a=b) ifTrue: [ aBlockCheckingConditions].
Boolean is still just an object, so you can use a cascade ^ (a=b) ifTrue: [ aBlockCheckingConditions]; yourself
But that will always return the the boolean from the first statement! If you want to return something from the conditions block you canât do that.
An alternative might be
^ a = b and: [ condition ]
Yes!! Good idea. Thanks. Anne
Peter
On Thu, Apr 2, 2015 at 6:02 PM, Max Leske <maxleske@gmail.com> wrote:
On 02 Apr 2015, at 17:31, Peter Uhnák <i.uhnak@gmail.com> wrote:
^ (a=b) ifTrue: [ aBlockCheckingConditions].
Boolean is still just an object, so you can use a cascade ^ (a=b) ifTrue: [ aBlockCheckingConditions]; yourself
But that will always return the the boolean from the first statement!
Ah, I've misread the question. Peter
its possible to completely or partly avoid a condition check . Instead you can use a common name for a method for various objects , that method may have different code for each object it belongs. This way you dont need to check your data instead you send the message and you let the method itself to execute the appropriate code for the approriate object( kind of data). This also much more reasonable to read. It will depend however in your particular situation. You can have also a chain of methods. Essentially we talk here about objects that interact with each other by triggering methods without the need for conditional checks since the method call is of the same name but the code executed depends on the receiver of the message / messages. It takes time to get used to this chain reaction message sending but I think it really worth it because it produces far simpler code to read and far more modular too. On Thu, Apr 2, 2015 at 6:15 PM, Anne Etien <anne.etien@univ-lille1.fr> wrote:
Hi,
I wanted to write a method that: - if (a = b) checks other conditions that are specified in the ifTrue block and return this (boolean) value - returns false if (a!=b)
So I wrote: myMethod ^ (a=b) ifTrue: [ aBlockCheckingConditions].
However, when (a!=b) the method returns nil (since false ifTrue: [] returns nil). So do you have an idea how I can do to express that or should I mandatorily wrote also an ifFalse:?
The problem comes certainly because we donât know that the ifTrueBlock will return a boolean.
If you have prettier ideas, I am interested in.
Anne
participants (5)
-
Anne Etien -
Clément Bera -
kilon alios -
Max Leske -
Peter Uhnák