On Sun, Jan 4, 2015 at 3:13 PM, Benoit St-Jean <[email protected]> wrote:
> In Object, it should return false, not self!
>
No. Look at Seb's original implementation:
Object>>ifNilOrEmpty: aBlock
self ifNil: [ ^ aBlock value ].
(self isCollection and: [
self isEmpty ]) ifTrue: [ ^ aBlock value ].
^ self.
The first statement moves to
UndefinedObject>>ifNilOrEmpty: aBlock ^aBlock value
The second to e.g.
Collection>>ifNilOrEmpty: aBlock
^self isEmpty ifTrue: [aBlock value] ifFalse: [self]
leaving just
Object>>ifNilOrEmpty: aBlock
^self
The idea is that in "var := self thing ifNilOrEmpty: [blah]" var gets "self
thing" if not nil or empty.
> -----------------
> Benoit St-Jean
> Yahoo! Messenger: bstjean
> Twitter: @BenLeChialeux
> Pinterest: benoitstjean
> IRC: lamneth
>
> Blogue: endormitoire.wordpress.com
>
> "A standpoint is an intellectual horizon of radius zero". (A. Einstein)
>
> ------------------------------
> *From:* Sebastian Sastre <[email protected]>
> *To:* Benoit St-Jean <[email protected]>; Pharo Development List <
> [email protected]>
> *Sent:* Sunday, January 4, 2015 6:06 PM
> *Subject:* Re: [Pharo-dev] Object>>ifNilOrEmpty: aBlock
>
> but not putting it on Object would change the feature since you *do want
> it* returning self on that case.
>
> But from your comment we can take the idea of adding it in Object,
> UndefinedObject and Collection and it will be a faster implementation since
> it will be trivial for the three of them, no?
>
>
>
>
>
> On Jan 4, 2015, at 8:40 PM, Benoit St-Jean <[email protected]> wrote:
>
> A cleaner implementation would be to define this method in UndefinedObject
> and Collection only. It should not appear in Object, the same way
> #isEmptyOrNil is defined.
>
> -----------------
> Benoit St-Jean
> Yahoo! Messenger: bstjean
> Twitter: @BenLeChialeux
> Pinterest: benoitstjean
> IRC: lamneth
> Blogue: endormitoire.wordpress.com
>
> "A standpoint is an intellectual horizon of radius zero". (A. Einstein)
>
> ------------------------------
> *From:* Sebastian Sastre <[email protected]>
> *To:* Pharo Development List <[email protected]>
> *Sent:* Sunday, January 4, 2015 5:27 PM
> *Subject:* [Pharo-dev] Object>>ifNilOrEmpty: aBlock
>
> Hi guys,
>
> I’ve started to use this little one:
>
> Object>>ifNilOrEmpty: aBlock
>
> self ifNil: [ ^ aBlock value ].
>
> (self isCollection and: [
> self isEmpty ]) ifTrue: [ ^ aBlock value ].
>
> ^ self.
>
>
> It allows you to do the widely known JavaScript one-liner:
>
> var stuff = this.thing || ‘some default value for when this.thing is
> undefined, null or an empty string’.
>
> but in smalltalk in this way:
>
> stuff := self thing ifNilOrEmpty: [ ‘some default value for when self
> thing is nil or an empty string’ ]
>
> simple thing feels practical and nice :)
>
>
>
>
>
>
>
--
best,
Eliot