[squeak-dev] Extending the functionality of caseOf: (was: #identityCaseOf:)
David T. Lewis
lewis at mail.msen.com
Sun Mar 28 20:22:53 UTC 2021
Changing the subject to capture Levente's interesting idea of extending
the functionality of caseOf: by passing the receiver object to the case
blocks as a parameter. This idea seems good enough to deserve its own
subject line.
Dave
On Sun, Mar 28, 2021 at 10:33:32AM +0200, Levente Uzonyi wrote:
> 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
>
More information about the Squeak-dev
mailing list
|