#valueWithExit inconsistency (?)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

#valueWithExit inconsistency (?)

Jaromir Matas
Current implementation of #valueWithExit seems a bit inconsistent in its
return value: it returns either nil (if the receiver exited using the exit
block) or the receiver.

Would it be worth modifying it to always return nil? (there are no senders
anyway)

valueWithExit
-  self value: [ ^nil ]
+  ^self value: [ ^nil ]

Or using a nice complementary method #valueWithExit:

valueWithExit: aBlock
        self value: [^aBlock value].
        ^aBlock value

valueWithExit
        ^self valueWithExit: [nil]

Thanks



-----
Jaromir
^[^
--
Sent from: http://forum.world.st/Squeak-Dev-f45488.html

^[^ Jaromir
Reply | Threaded
Open this post in threaded view
|

Re: #valueWithExit inconsistency (?)

marcel.taeumel
Hi Jaromir.

I added commentary via Kernel-mt.1370. I don't think that the (outer block's) return value should ever be used in this construct. It is more like a #detect: but without meaningful return values.

http://forum.world.st/explicit-return-from-a-block-tp51833.html
http://forum.world.st/The-Trunk-Kernel-ul-519-mcz-td3077583.html

I don't suppose it is idiomatic Smalltalk: "...but actually, I never had any reason to use this except when porting algorithms from Fortran or C... "

Best,
Marcel

Am 16.02.2021 14:05:23 schrieb Jaromir <[hidden email]>:

Current implementation of #valueWithExit seems a bit inconsistent in its
return value: it returns either nil (if the receiver exited using the exit
block) or the receiver.

Would it be worth modifying it to always return nil? (there are no senders
anyway)

valueWithExit
- self value: [ ^nil ]
+ ^self value: [ ^nil ]

Or using a nice complementary method #valueWithExit:

valueWithExit: aBlock
self value: [^aBlock value].
^aBlock value

valueWithExit
^self valueWithExit: [nil]

Thanks



-----
Jaromir
^[^
--
Sent from: http://forum.world.st/Squeak-Dev-f45488.html



Reply | Threaded
Open this post in threaded view
|

Re: #valueWithExit inconsistency (?)

Eliot Miranda-2


On Wed, Feb 17, 2021 at 8:52 AM Marcel Taeumel <[hidden email]> wrote:
Hi Jaromir.

I added commentary via Kernel-mt.1370. I don't think that the (outer block's) return value should ever be used in this construct. It is more like a #detect: but without meaningful return values.


I don't suppose it is idiomatic Smalltalk: "...but actually, I never had any reason to use this except when porting algorithms from Fortran or C... "

But there's a bug.  It should at least answer the result of value:. I'm going to change it.
Best,
Marcel

Am 16.02.2021 14:05:23 schrieb Jaromir <[hidden email]>:

Current implementation of #valueWithExit seems a bit inconsistent in its
return value: it returns either nil (if the receiver exited using the exit
block) or the receiver.

Would it be worth modifying it to always return nil? (there are no senders
anyway)

valueWithExit
- self value: [ ^nil ]
+ ^self value: [ ^nil ]

Or using a nice complementary method #valueWithExit:

valueWithExit: aBlock
self value: [^aBlock value].
^aBlock value

valueWithExit
^self valueWithExit: [nil]

Thanks



-----
Jaromir
^[^
--
Sent from: http://forum.world.st/Squeak-Dev-f45488.html




--
_,,,^..^,,,_
best, Eliot


Reply | Threaded
Open this post in threaded view
|

Re: #valueWithExit inconsistency (?)

Jaromir Matas
Hi, thanks for your feedback! I'm by no means advocating for using returns
from blocks :) I just found it useful in understanding the local vs.
non-local returns... e.g. in this example the return value is useful:

"find max value before nil and return its square"
| supplier max maxSquared |
supplier := {3 . 2 . 5 . 1 . nil . 6} readStream.
maxSquared := [:exitBlock | | value |
        max := 0.
        [supplier atEnd]
                whileFalse: [
                        value := supplier next.
                        value ifNil: [exitBlock value].
                        max := max max: value]
] valueWithExit: [max squared].
"more code"
Transcript show: maxSquared






-----
Jaromir
^[^
--
Sent from: http://forum.world.st/Squeak-Dev-f45488.html

^[^ Jaromir