[squeak-dev] Extending the functionality of caseOf: (was: #identityCaseOf:)

Chris Muller asqueaker at gmail.com
Sun Mar 28 22:26:42 UTC 2021


> > 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.


Here's the version using a temporary and existing API (with an added
identity-check).

   result := self someComplexExpression.
   true caseOf:
      { [result=1] -> [self success].
      [result odd] -> [self processEvenValue: result-1].
      [ result == identityValue ] -> [self processSomethingElse ].
      [true] -> [self processEvenValue] }

"true caseOf:" is my sneaky way to improve its flexibility.  The suggested
expansion of the API seems too dilute, not enough bang.


> The
> > compiler could do exactly that behind the scenes.
> > - when the value is passed to the matcher block, possibilities are
> greatly
> > extended.


The evaluation is done before any of the caseOf tests, so is it really
extended with more expressive power?  Or just a different syntax that moves
the variable declaration from a temporary to the block arg?


> 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
> >
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20210328/631a68b5/attachment.html>


More information about the Squeak-dev mailing list