[Cuis-dev] unnecessary punctuation

Gerald Klix cuis.01 at klix.ch
Thu Jun 13 01:57:45 PDT 2024



On 6/12/24 3:56 PM, Mark Volkmann via Cuis-dev wrote:
> I saw this example in a YouTube video about Smalltalk this morning:
>
> (2 > 3) ifTrue: ['Yes'] ifFalse: ['No']
>
> It seems to me that none of the parentheses or square brackets are needed
> here.
> The following works the same for me.
>
> 2 > 3 ifTrue: 'Yes' ifFalse: 'No'
>
> Is it true that keyword messages that take no-arg blocks can always take a
> literal value instead?
> It seems this works because the `Object` class defines the `value` method
> to just return `self`.
>
>
You are right.

AFAIK, not every Smalltalk has a Object>>#value method that returns self.
This works with Squeak, Cuis and perhaps Pharo.

The byte-code generated for:

2 > 3 ifTrue: 'Yes' ifFalse: 'No'

57 <77> pushConstant: 2
58 <21> pushConstant: 3
59 <B3> send: >
60 <22> pushConstant: 'Yes'
61 <23> pushConstant: 'No'
62 <F0> send: ifTrue:ifFalse:
63 <7C> returnTop


is less efficient than the byte-code for

2 > 3 ifTrue: [ 'Yes' ] ifFalse: [ 'No' ]

57 <77> pushConstant: 2
58 <22> pushConstant: 3
59 <B3> send: >
60 <99> jumpFalse: 63
61 <21> pushConstant: 'Yes'
62 <90> jumpTo: 64
63 <20> pushConstant: 'No'
64 <7C> returnTop


You might notice that sends of #ifTrue:ifFalse: are compiled
to byte-code-jumps , if they contain literal blocks:


| yesBlock noBlock |
yesBlock := [ 'Yes' ].
noBlock := [ 'No' ].
2 > 3 ifTrue: yesBlock ifFalse: noBlock

57 <8F 00 00 02> closureNumCopied: 0 numArgs: 0 bytes 61 to 62
61     <20> pushConstant: 'Yes'
62     <7D> blockReturn
63 <68> popIntoTemp: 0
64 <8F 00 00 02> closureNumCopied: 0 numArgs: 0 bytes 68 to 69
68     <21> pushConstant: 'No'
69     <7D> blockReturn
70 <69> popIntoTemp: 1
71 <77> pushConstant: 2
72 <23> pushConstant: 3
73 <B3> send: >
74 <10> pushTemp: 0
75 <11> pushTemp: 1
76 <F2> send: ifTrue:ifFalse:
77 <7C> returnTop


@Juan: If just found that stepping *through* this expression
in the debugger does not work with Cuis/Haver version 6350.
Please try it and see for yourself.


HTH,

Gerald



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240613/9bb5f5df/attachment.htm>


More information about the Cuis-dev mailing list