4 ms·
If you want extreme case of removing all ifs, see how ifTrue: and friends are implemented in Smalltalk (ie. true and false are instances of different classes),
by dfox 10y ago
If you want extreme case of removing all ifs, see how ifTrue: and friends are implemented in Smalltalk (ie. true and false are instances of different classes), although essentially all compiler implementations turn that into normal conditional branches in generated bytecode.
- chriswarbo 10y agoAlso, from the functional programming side, Church Booleans: function true(x, y) { return x; } function false(x, y) { return y; } There's an example of Church encoding used in anger at http://www.haskellforall.com/2016/04/data-is-code.html http://www.haskellforall.com/2016/04/data-is-code.html
- aaron-santos 10y agoI think I'm looking at an implementation of ifTrue[1], but it's a bit dense to someone who's not familiar with Smalltalk. Maybe you could walk me through it? [1] - http://git.savannah.gnu.org/gitweb/?p=smalltalk.git;a=blob;f=packages/stinst/parser/STCompiler.st;hb=HEAD#l842 http://git.savannah.gnu.org/gitweb/?p=smalltalk.git;a=blob;f...
- dfox 10y agoThat's the compiler part that optimizes the elegance away :). The interesting part is here [1], note that it will work without any support from the compiler. [1] - http://git.savannah.gnu.org/gitweb/?p=smalltalk.git;a=blob;f=kernel/False.st;h=b1a85e58e6e90bc2034bf653d6276d12b8d1dae6;hb=HEAD http://git.savannah.gnu.org/gitweb/?p=smalltalk.git;a=blob;f... and http://git.savannah.gnu.org/gitweb/?p=smalltalk.git;a=blob;f=kernel/True.st;h=eab5d9b9f11c501598115916dc234f8f50580c1f;hb=HEAD http://git.savannah.gnu.org/gitweb/?p=smalltalk.git;a=blob;f...
- aaron-santos 10y agoMuch more straight forward. If I'm reading it correctly, True and False are subclasses of Boolean and each of which implements a complementary pair of ifTrue and ifFalse methods? Elegantly symmetric. :)