Skip to content

Instantly share code, notes, and snippets.

@uliwitness
Created August 28, 2017 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uliwitness/053c930eb7a3af5c5e104a7dfdc93a31 to your computer and use it in GitHub Desktop.
Save uliwitness/053c930eb7a3af5c5e104a7dfdc93a31 to your computer and use it in GitHub Desktop.
Still fascinating how Smalltalk implements if/then/else. Basically it goes like this (in pseudo-Swift):
class BooleanClass {
void ifTrue(_ trueHandler: () -> Void, else falseHandler: () -> Void)
}
class TrueBooleanClass {
void ifTrue(_ trueHandler: () -> Void, else falseHandler: () -> Void) {
trueHandler()
}
}
class FalseBooleanClass {
void ifTrue(_ trueHandler: () -> Void, else falseHandler: () -> Void) {
falseHandler()
}
}
let True = TrueBooleanClass()
let False = FalseBooleanClass()
let result: BooleanClass = someFunctionThatReturnsTrueOrFalse()
result.ifTrue( { in
print("The result was True")
}, else: { in
print("The result was False") }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment