3 ms·
In Smalltalk those methods don't return `true`. They take a block and evaluate it if the boolean receiving the message (a > b) ifTrue: [ "do something" ]
by wild_egg 1y ago
In Smalltalk those methods don't return `true`. They take a block and evaluate it if the boolean receiving the message
(a > b) ifTrue: [ "do something" ]
EDIT: to clarify what's happening there, `>` is a message sent to `a` that will result in a boolean. The True class and False class both understand the ifTrue: message and `True>>ifTrue:` executes the block whereas `False>>ifTrue:` just throws it away.
There's no `if` keyword in the language. Control flow is done purely through polymorphism.
- chao- 1y agoI apologize for my lack of Smalltalk knowledge. As you can imagine, you can do similar in Ruby by defining ifTrue to accept a block, even adding ifTrue on other all objects and defining something similar: class TrueClass def ifTrue(&block) = block.call end class FalseClass def ifTrue(&block) = nil end class Object def ifTrue(&block) = block.call end class NilClass def ifTrue(&block) = nil end If ck45's core complaint was that this is not baked into the language, I will agree that it is less convenient for lack of a default.
- oezi 1y agoCertainly possible: add ifTrue as a method to TrueClass and FalseClass. It just isn't very fast.
- codesnik 1y agoproblem is not with ifTrue, and not with it's performance, it's easy to do. it is "ifTrue:ifFalse:" also it is common to do assignments in the "if", and with actual method and blocks scope of the introduced variable would be different and everyone would be tripping on it all the time.