4 ms·
One difference is that they are used for all control structures: a = b ifTrue:[ ... ] a = b ifFalse:[ ... ] a = b ifTrue:[ ...] ifFalse:[ ... ]
by mpweiher 5y ago
One difference is that they are used for all control structures:
a = b ifTrue:[ ... ]
a = b ifFalse:[ ... ]
a = b ifTrue:[ ...] ifFalse:[ ... ]
[ a = b ] whileTrue: [ ... ]
And "control structures" aren't actually control structures that are built into the language. They are just messages sent to blocks (closures). That includes iteration, so a for "loop" is:
1 to:10 do: [ :i | ... ]
Which is just a shorthand for
(1 to:10) do:[ :i | ... ]
With (1 to:10) creating an Interval object, a virtual collection that understands the normal collection iteration protocol (do:, collect:, select:, reject:, inject: ).
OO + Closures + keyword syntax. An elegant and powerful combination.
- afandian 5y agoIt's been a while but this looks very much like Ruby to me.
- projektfu 5y agoNot “part of the language” but definitely privileged in the implementation. #ifTrue:ifFalse: gets rewritten and inlined as a branch in just about every compiler. If you implemented your own, it would suffer in performance.