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

Thiede, Christoph Christoph.Thiede at student.hpi.uni-potsdam.de
Wed Apr 7 16:15:57 UTC 2021


Hi Levente,


IMHO a DSL is very much about syntactic sugar. If we obviate it, we impede the development of richer and higher-level language concepts that abstract from repetitive and uninteresting basics such as the temporary variable in your example. Just my two cents. :-)


Best,

Christoph

________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Levente Uzonyi <leves at caesar.elte.hu>
Gesendet: Dienstag, 30. März 2021 22:21:00
An: ma.chris.m at gmail.com; The general-purpose Squeak developers list
Betreff: Re: [squeak-dev] Extending the functionality of caseOf: (was: #identityCaseOf:)

Hi Chris,

On Sun, 28 Mar 2021, Chris Muller wrote:

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

Yes, it does the job, but it's a bit more verbose and probably less
efficient: you have to declare the variable yourself, and you have to
write result=1 instead of just 1.

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

It's just syntactic sugar. The optimized evaluation would just reuse the
existing temporary instead of creating a new one all the time.
The default implementation would be

Object >> caseOf: aBlockAssociationCollection otherwise: aBlock

         aBlockAssociationCollection associationsDo:
                 [:assoc | (assoc key numArgs = 0
                         ifTrue: [ assoc key value = self ]
                         ifFalse: [ assoc key value: self ]) ifTrue: [^assoc value cull: self]].
         ^ aBlock cull: self


Levente

>
>       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/20210407/502e2ce8/attachment.html>


More information about the Squeak-dev mailing list