[squeak-dev] #identityCaseOf:
Levente Uzonyi
leves at caesar.elte.hu
Sun Mar 28 08:33:32 UTC 2021
Hi Christoph,
I see some value having #identityCaseOf:, so +1 from me.
But what would be a more interesting thing to explore (yes, I'm derailing
this conversation now because the answer to your question is yes already
:)) is to extend the functionality of #caseOf:.
For example, passing the object to the condition block (or even the result
block) could be useful:
self someComplexExpression caseOf: {
[ 1 ] -> [ self success ]
[ :value | value odd ] -> [ :value | self processEvenValue: value - 1 ].
[ :value | true "it's even ] -> [ :value | self processEvenValue ] }
There are two new things in the example:
- the value of self someComplexExpression is optionally passed to the
blocks. Yes, that could be done by creating a temporary variable. The
compiler could do exactly that behind the scenes.
- when the value is passed to the matcher block, possibilities are greatly
extended. For example, it could even reproduce #identityCaseOf:
foo caseOf: {
[ :o | o == #foo ] -> [ self foo ].
[ :o | o == #bar ] -> [ self bar ] }
The same thing could be done with the otherwise block too:
self foo
caseOf: { ... }
otherwise: [ :value | value ]
Levente
On Sat, 27 Mar 2021, Thiede, Christoph wrote:
>
> Hi all,
>
>
> the proposal is in the title. :-) Do you think we could need something like #identityCaseOf:[otherwise:], analogously to #caseOf:[otherwise:], on Object?
>
>
> Here is my motivation: I very often use #caseOf:[otherwise:] to eliminate nested if blocks (for one of many examples, take a look at PreferenceBrowserMorph >> #keyPressed:). In one recent situation [1], however, I confused
> equality and identity (I have to admit that this is a common error of mines) so I had to convert my beautiful #caseOf: statement back to a boring list of if checks. I wonder whether we could - or should - introduce something
> like #identityCaseOf:otherwise: (names subjected to discussion) for such situations?
>
>
> Historically, I guess this has not been built because #caseOf: has probably been inspired by the switch/select/case statement of other languages, which usually only accept constant case values. But in Smalltalk, luckily, we
> do not have this restriction, thus I'm wondering whether there is any reason to have #caseOf: but not #identityCaseOf:. Looking forward to your opinions! :-)
>
>
> Best,
>
> Christoph
>
>
> [1] https://github.com/LinqLover/SimulationStudio/commit/ddf0ef3e21c63a2fd9b08703c8e9ff213b7b6b0b
>
>
More information about the Squeak-dev
mailing list
|