Quoting constructs: Difference between revisions
Content added Content deleted
(→Blocks) |
(→Blocks) |
||
Line 443: | Line 443: | ||
<lang smalltalk>aBlock value. "evaluate the block, passing no argument" |
<lang smalltalk>aBlock value. "evaluate the block, passing no argument" |
||
anotherBlock value:1 value:2. "evaluate the block, passing two arguments"</lang> |
anotherBlock value:1 value:2. "evaluate the block, passing two arguments"</lang> |
||
The most basic implementation of such a control structure is found in the Boolean subclasses True and False, which implement eg. "ifTrue:arg" and "ifFalse:". Here are those two as concrete example: |
The most basic implementation of such a control structure is found in the Boolean subclasses True and False, which implement eg. "<tt>ifTrue:arg</tt>" and "<tt>ifFalse:</tt>". Here are those two as concrete example: |
||
<lang smalltalk>in the True class: |
<lang smalltalk>in the True class: |
||
ifTrue: aBlock |
ifTrue: aBlock |
||
Line 451: | Line 451: | ||
ifTrue: aBlock |
ifTrue: aBlock |
||
^ nil "I am false, so I ignore the block"</lang> |
^ nil "I am false, so I ignore the block"</lang> |
||
Thus, the expression "someBoolean ifTrue:[ 'hello print' ]" will either evaluate the lambda or not, depending on the someBoolean receiver. |
Thus, the expression <tt>"someBoolean ifTrue:[ 'hello print' ]"</tt> will either evaluate the lambda or not, depending on the someBoolean receiver. |
||
Obviously, you can teach other objects on how to respond to "value" messages and then use them as if they where blocks. |
Obviously, you can teach other objects on how to respond to "value" messages and then use them as if they where blocks. |
||
Actually, the Object class also implements "value", so you can also write: |
Actually, the Object class also implements "value", so you can also write: |
||
< |
"<tt>a := someCondition ifTrue:10 ifFalse:20</tt>". |
||
Thich works because "<tt>Object value</tt>" simply returns the receiver. |
|||
In addition, some Smalltalk dialects implement additional syntax extensions. |
In addition, some Smalltalk dialects implement additional syntax extensions. |