Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
This post was updated on Oct 31, 2016; 1:24pm.
CONTENTS DELETED
The author has deleted this message.
|
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
5697 posts
|
> On 31 Oct 2016, at 14:17, CodeDmitry <[hidden email]> wrote: > > I am looking at > > ifTrue: alternativeBlock > "If the receiver is false (i.e., the condition is false), then the value is > the > false alternative, which is nil. Otherwise answer the result of evaluating > the argument, alternativeBlock. Create an error notification if the > receiver is nonBoolean. Execution does not actually reach here because > the expression is compiled in-line." > > self subclassResponsibility > > In order to perform the block, ifTrue must somehow end up evaluating the > block, but this code only sends a subclassResponsibility message to itself > and implicitly returns itself. > > Where does the block actually get evaluated? ... [show rest of quote] In the sub classes of Boolean, True and False. Have a look at ALL implementors of #ifTrue: or #ifFalse: There is only one instance of True, the constant true, same for False and false. HTH, Sven > > > > -- > View this message in context: http://forum.world.st/How-does-Boolean-ifTrue-work-tp4920873.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. > |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
1067 posts
|
In reply to this post by CodeDmitry
On 31/10/2016 14:17, CodeDmitry wrote:
> I am looking at > > ifTrue: alternativeBlock > "If the receiver is false (i.e., the condition is false), then the value is > the > false alternative, which is nil. Otherwise answer the result of evaluating > the argument, alternativeBlock. Create an error notification if the > receiver is nonBoolean. Execution does not actually reach here because > the expression is compiled in-line." > > self subclassResponsibility > > In order to perform the block, ifTrue must somehow end up evaluating the > block, but this code only sends a subclassResponsibility message to itself > and implicitly returns itself. > > Where does the block actually get evaluated? > > ... [show rest of quote] ... [show rest of quote] Hi!
Boolean is only an abstract class. You never manipulate a Boolean, you manipulate True or False. The #ifTrue: method works with a dispatch between True and False. Try to check: `True browse` or `False browse` :) > > > -- > View this message in context: http://forum.world.st/How-does-Boolean-ifTrue-work-tp4920873.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. > -- Cyril Ferlicot http://www.synectique.eu 2 rue Jacques Prévert 01, 59650 Villeneuve d'ascq France |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
104 posts
|
In reply to this post by CodeDmitry
Well, in fact Boolean class has no instances, the boolean value true is an instance of True class, you should look at the ifTrue: methods in the subclasses of Boolean True and False. But also, ifTrue: expressions are inlined, as the comment says in the code you posted. So I think that all this ifTrue: methods are never actually executed. 2016-10-31 14:17 GMT+01:00 CodeDmitry <[hidden email]>: I am looking at ... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
This post was updated on Oct 31, 2016; 1:56pm.
In reply to this post by CodeDmitry
CONTENTS DELETED
The author has deleted this message.
|
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
1618 posts
|
Hi Dmitry,
you found it yourself, sort of! Le 31/10/2016 à 14:25, CodeDmitry a écrit : > Wait, so unlike JavaScript; True and False are actual singletons and > ifTrue/ifFalse/ifTrue:ifFalse: uses visitor pattern to have the actual True > or False handle the block rather than doing it themselves? Yes, true and false are singletons (resp single instance of the True class, False class). No, this is not a visitor pattern, just polymorphism (True and False have their own implementation of ifTrue:/ifFalse:/ifTrue:ifFalse:/ifFalse:ifTrue:). Regards, Thierry |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
1922 posts
|
In reply to this post by CodeDmitry
When I teach Smalltalk, this is one of the most beautiful exercises for
students. Those who get it will love Smalltalk, those who don't often leave the project soon ;-) BTW: This is not a visitor or any other pattern, it really is just Polymorphism at work and the reason why back in the 90ies, people said if statements should be replaced by Polymorphism Joachim Am 31.10.16 um 14:17 schrieb CodeDmitry: > I am looking at > > ifTrue: alternativeBlock > "If the receiver is false (i.e., the condition is false), then the value is > the > false alternative, which is nil. Otherwise answer the result of evaluating > the argument, alternativeBlock. Create an error notification if the > receiver is nonBoolean. Execution does not actually reach here because > the expression is compiled in-line." > > self subclassResponsibility > > In order to perform the block, ifTrue must somehow end up evaluating the > block, but this code only sends a subclassResponsibility message to itself > and implicitly returns itself. > > Where does the block actually get evaluated? > > > > > -- > View this message in context: http://forum.world.st/How-does-Boolean-ifTrue-work-tp4920873.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. > > ... [show rest of quote] -- ----------------------------------------------------------------------- Objektfabrik Joachim Tuchel mailto:[hidden email] Fliederweg 1 http://www.objektfabrik.de D-71640 Ludwigsburg http://joachimtuchel.wordpress.com Telefon: +49 7141 56 10 86 0 Fax: +49 7141 56 10 86 1 |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
This post was updated on Oct 31, 2016; 2:26pm.
In reply to this post by Thierry Goubier
CONTENTS DELETED
The author has deleted this message.
|
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
1618 posts
|
Le 31/10/2016 à 14:58, CodeDmitry a écrit :
> But still, how is the actual argument "alternativeBlock" passed to the > True/False from a Boolean? > > The message does not cache the message inside itself before passing the > message, and it does not pass the alternative block along with the message. If I get correctly what you are saying, this is because the ifTrue:ifFalse: message is never sent to an instance of Boolean. It is sent either to true, the single instance of class True, or to false, the single instance of class False, where the implementation is what you expect: False>>#ifTrue:ifFalse: ifTrue: trueAlternativeBlock ifFalse: falseAlternativeBlock "Answer the value of falseAlternativeBlock. Execution does not actually reach here because the expression is compiled in-line." ^falseAlternativeBlock value If you're interested in the details, do also read the comment. Regards, Thierry |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
5697 posts
|
In reply to this post by CodeDmitry
myCollection isEmpty ifTrue: [ self inform: 'The collection is empty' ].
First #isEmpty is sent to myCollection which results in either true or false as value. Then #ifTrue: is sent to this value. Now, if the value was true (the sole instance of class True), the code in True>>#ifTrue: is executed, which will evaluate the block by sending it #value. If the value was false, the code in False>>#ifTrue: is executed, and nil is returned and the block is not executed. Like others said, it is just polymorphism at work. (And yes, the compiler optimises some of this logic a bit, but the effect is the same). > On 31 Oct 2016, at 14:58, CodeDmitry <[hidden email]> wrote: > > But still, how is the actual argument "alternativeBlock" passed to the > True/False from a Boolean? > > The message does not cache the message inside itself before passing the > message, and it does not pass the alternative block along with the message. > > > > -- > View this message in context: http://forum.world.st/How-does-Boolean-ifTrue-work-tp4920873p4920886.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. > |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
This post was updated on Oct 31, 2016; 2:41pm.
In reply to this post by Thierry Goubier
CONTENTS DELETED
The author has deleted this message.
|
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
3147 posts
|
In reply to this post by Sven Van Caekenberghe-2
but you should use myCollection ifEmpty: [ ... ] Phil On Mon, Oct 31, 2016 at 3:25 PM, Sven Van Caekenberghe <[hidden email]> wrote: myCollection isEmpty ifTrue: [ self inform: 'The collection is empty' ]. ... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
3147 posts
|
In reply to this post by CodeDmitry
For the full story, these objects and ifTrue: etc are special cases in the VM code. For speed. Phil On Mon, Oct 31, 2016 at 3:32 PM, CodeDmitry <[hidden email]> wrote: I realized it a bit before you posted but thanks for confirming. ... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
1618 posts
|
In reply to this post by CodeDmitry
Le 31/10/2016 à 15:32, CodeDmitry a écrit :
> I realized it a bit before you posted but thanks for confirming. > > Boolean seems to indeed just be a "Dynamic Interface"; since Smalltalk does > not have "Java Interfaces"(nor want them for the same reason JavaScript > doesn't), the developers wanted to still have True and False be subclasses > of Booleans. Boolean is a nice header for classification. Everybody is familiar with Boolean algebra, including hardware guys :) > In all honesty Boolean seems to be there purely for the "common sense" of > it, rather than need. Smalltalk is duck typed and has no need for > interfaces. If it's a Boolean, you know it has those methods so Boolean is > not actually useful for anything; True and False may as well just be > subclasses of Object. Not exactly. Boolean carry 15 methods that are not overridden in either False nor True (and 11 methods which are), so Boolean is also used as a way to share code between the classes True and False and avoid code duplication. A nice case for having a common superclass below Object. Thierry |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
This post was updated on Oct 31, 2016; 3:04pm.
CONTENTS DELETED
The author has deleted this message.
|
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
2343 posts
|
In reply to this post by CodeDmitry
2016-10-31 11:32 GMT-03:00 CodeDmitry <[hidden email]>:
> I realized it a bit before you posted but thanks for confirming. > > Boolean seems to indeed just be a "Dynamic Interface"; since Smalltalk does > not have "Java Interfaces"(nor want them for the same reason JavaScript > doesn't), the developers wanted to still have True and False be subclasses > of Booleans. > > In all honesty Boolean seems to be there purely for the "common sense" of > it, rather than need. Smalltalk is duck typed and has no need for > interfaces. If it's a Boolean, you know it has those methods so Boolean is > not actually useful for anything; True and False may as well just be > subclasses of Object. Not exactly, look at the methods in Boolean and you'll see many methods are common to all. So Boolean has a reason to be the superclass of both True and False. Esteban A. Maringolo |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
3479 posts
|
In reply to this post by philippeback
Why? Norbert
... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
2343 posts
|
In reply to this post by CodeDmitry
2016-10-31 11:58 GMT-03:00 CodeDmitry <[hidden email]>:
> Ah so it's more of a "dynamic abstract class" than a "dynamic interface", so > they do get visited during the traversal of the inheritence chain(rather > than skipped immediately to Object). I don't know what you mean by "dynamic". The whole object system of Smalltalk is dynamic by design. Esteban A. Maringolo |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
2343 posts
|
In reply to this post by NorbertHartl
2016-10-31 12:11 GMT-03:00 Norbert Hartl <[hidden email]>:
> > Am 31.10.2016 um 15:59 schrieb [hidden email]: > > but you should use myCollection ifEmpty: [ ... ] > > Why? It enables you to implement polymorphism with an object that might not be aCollection, but behaves like one. Of course you could also implement #isEmpty, but in my experience the less you test (#isEmpty) and the more you delegate (ifEmpty:) the better. :) Regards, Esteban A. Maringolo |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
2224 posts
|
In reply to this post by NorbertHartl
2016-10-31 16:11 GMT+01:00 Norbert Hartl <[hidden email]>: Why? because it shorter :) |
Free forum by Nabble | Edit this page |