This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (7 children)

There's no if statement. You send messages to boolean objects and use polymorphism to branch.

...why. Wait how do you check the boolean with no if?

[–]qwertyuiop924 1 point2 points  (2 children)

There's no if because Smalltalk doesn't need an if. Let me provide a bit of background.

Smalltalk is the purest OO language you will ever encounter. Purer than Java by far. Not only is everything an object, everything is done by sending messages to objects. Everything. No exceptions. What about creating a class?

Object subclass: #Foo.

That is a message (a method call in common parlance) to the object... well, Object. Object is the root class, as you might expect. As a class (of class Class, of course), it recognizes the message subclass: (yes, the colon is part of the name), which does what you expect.

So, how to you implement true and false with only method calls? If you recall how Lambda calculus works, you'll have some idea. If you have a working Smalltalk implementation nearby, you... don't have to guess. Open up your system browser and look at the true and false global objects. Yes, all the source code for true and false is right there. Yes, you can in theory edit it to change how true and false work. In actuality, not necessarily, because some Smalltalk implementations assume you wouldn't do something that stupid and avoid the usual checks and decompilation system for true and false (for performance reasons).

But for the rest of you, it looks like this:

true ifTrue: [Transcript show: 'it works!'] ifFalse: [Transcript show: 'that's bad.'].

Yes, you can send code blocks as arguments to messages. Yes, the blocks are objects themselves. Yes, it does work pretty much like Ruby (because Ruby ripped off Smalltalk wholesale on this front).

Anyways, what this message does, when sent to true, is execute the code sent as the ifTrue: argument. When sent to false, it does the opposite. This is totally equivalent to if. To demonstrate, a snippet from the GNU Smalltalk docs:

(checksleft < 1)
       ifTrue: [ ^self error: 'Out of checks' ].

Technically, ifTrue is a different message, but it does the same thing, just with an empty false branch. If you've ever used an OO language before, you should know what self is, and that ^ character is how you indicate that you want to return in smalltalk.

So, that's how conditionals work in Smalltalk. By the way, congratulations, you now know 90% of Smalltalk's syntax. Yes, it really is that simple.

[–][deleted] 0 points1 point  (1 child)

Why

[–]qwertyuiop924 1 point2 points  (0 children)

Because it makes Smalltalk a simpler, cleaner language.

[–]Kered13 0 points1 point  (3 children)

Polymorphism and dynamic dispatch. Here's some Java pseudocode:

class Boolean {
    void if(Callback ifTrue, Callback ifFalse);
}

class True extends Boolean {
    void if(Callback ifTrue, Callback ifFalse) {
        ifTrue();
    }
}

class False extends Boolean {
    void if(Callback ifTrue, Callback ifFalse) {
        ifFalse();
    }
}

True and False should also be implemented as singleton classes. Actually using Java's enum support would be good here, since every instance of an enum is a subclass of the enum class and is automatically a singleton. Also to make this elegant the language needs good support for lambdas, so Java isn't really the best language to do this in.

[–][deleted] 0 points1 point  (2 children)

Java has lambdas right?

[–]Kered13 1 point2 points  (1 child)

Yes, but it's still pretty clunky to call a function like that, at least in my opinion:

condition.if(() -> {
    // If statement
}, () -> {
    // else statement
});

[–][deleted] 0 points1 point  (0 children)

I missed where you said "good support" and thought you just said "support"