[squeak-dev] Question about #caseOf:otherwise:

Levente Uzonyi leves at caesar.elte.hu
Thu Aug 15 11:32:14 UTC 2019


On Thu, 15 Aug 2019, Thiede, Christoph wrote:

> 
> Sounds very interesting :)
> 
> 
> In the past I have been writing the following instead:
> 
> 
> true caseOf: {
> 
>     [aNumber isEven] -> ['That is an even number.'].
> 
>     [aNumber isOdd] -> ['That is an odd number.']. 
> 
> }.
> 
> 
> but your code is much more beautiful :)
> 
> I think both variants have a contrary semantic, but on the other hand it just reads intuitive:
> 
> 
> aNumber caseOf: {
>    [42] -> ['That is correct.'].
>    [:n | n even] -> ['That is an even number.'].
>    [:n | n odd] -> ['That is an odd number.'].
> }.
> 
> 
> This would also require some changes to the Parser. If your examples compiles, I would also expect the following to run:
> 
> aNumber caseOf: {
>    #even -> ['That is an even number.'].
>    #odd -> ['That is an odd number.'].
> }.

While you could expect that to compile, the Parser is strict about the 
format, because everything (blocks, associations, the array) will be 
inlined, so no objects will be created for them. Without inlining, such 
construct would not be viable performance-wise.

Levente

> 
> Best,
> Christoph
> 
> _________________________________________________________________________________________________________________________________________________________________________________________________________________________________
> Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Taeumel, Marcel
> Gesendet: Donnerstag, 15. August 2019 10:32:07
> An: gettimothy via Squeak-dev
> Betreff: [squeak-dev] Question about #caseOf:otherwise:  
> Hi all!
> 
> Why isn't the following example possible?
> 
> aNumber caseOf: {
>    [:n | n even] -> ['That is an even number.'].
>    [:n | n odd] -> ['That is an odd number.'].
> }.
> 
> That is, why is the check "assoc key value = self" not configurable as "(x := assoc key cull: self) == true or: [x = self]"? :-)
> 
> ... maybe it is a good thing that case-of statements in Smalltalk are not that powerful?
> 
> Best,
> Marcel
> 
>


More information about the Squeak-dev mailing list