Null Coalesce Operator?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Null Coalesce Operator?

Aliaksei Syrel
Hi,

Null coalesce operator - returns the first not nil operand.

Adding one simple method to an Object:

?? anObject
 ^ self

and to UndefinedObject:

?? anObject
 ^ anObject

could allow us to write: 
 
default1 := nil.
default2 := nil.
default3 := 'foo'.
default4 := 'don''t care'.
config := default1 ?? default2 ?? default3 ?? default4.

=> 'foo' 

I know you don't like to add new methods to an object, but still :)

Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

Sergio Fedi

In my experience those situations end up been handled by collections, detect:ifNone: which even handles the case when all defaults are nil.

But maybe that idiom is very common in another situations.
Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

abergel
In reply to this post by Aliaksei Syrel
Hi!

I perfectly understand the purpose of this method "??”
Although I do not think this will seriously impact the quality of the code being produced, I still believe it will not help making code of a better quality. Check for the nil value should be banned in my opinion. There are well-known techniques to not have to use it (e.g., Null-object pattern).

Cheers,
Alexandre

-- 
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel  http://www.bergel.eu
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.



On May 20, 2015, at 3:19 PM, Aliaksei Syrel <[hidden email]> wrote:

Hi,

Null coalesce operator - returns the first not nil operand.

Adding one simple method to an Object:

?? anObject
 ^ self

and to UndefinedObject:

?? anObject
 ^ anObject

could allow us to write: 
 
default1 := nil.
default2 := nil.
default3 := 'foo'.
default4 := 'don''t care'.
config := default1 ?? default2 ?? default3 ?? default4.

=> 'foo' 

I know you don't like to add new methods to an object, but still :)


Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

Damien Pollet
In reply to this post by Sergio Fedi
…ifNone: is a nice idiom, but not always available (in which case, we should think about introducing it)

?? is basically an infix but eager version of #ifNil:, IMHO a pragmatic thing to have; could be quite handy.
My only fear is that it could encourage the use of nil instead of proper Null Objects… and here we start bikeshedding

On 20 May 2015 at 21:31, Sergio Fedi <[hidden email]> wrote:

In my experience those situations end up been handled by collections, detect:ifNone: which even handles the case when all defaults are nil.

But maybe that idiom is very common in another situations.



--
Damien Pollet
type less, do more [ | ] http://people.untyped.org/damien.pollet
Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

Sven Van Caekenberghe-2
Yes, #ifNil: is almost the same (except the #value):

|default1 default2 default3 default4 |
default1 := nil.
default2 := nil.
default3 := 'foo'.
default4 := 'don''t care'.
default1 ifNil: [ default2 ifNil: [ default3 ifNil: [ default4 ] ] ]

=> 'foo'

> On 20 May 2015, at 21:38, Damien Pollet <[hidden email]> wrote:
>
> …ifNone: is a nice idiom, but not always available (in which case, we should think about introducing it)
>
> ?? is basically an infix but eager version of #ifNil:, IMHO a pragmatic thing to have; could be quite handy.
> My only fear is that it could encourage the use of nil instead of proper Null Objects… and here we start bikeshedding
>
> On 20 May 2015 at 21:31, Sergio Fedi <[hidden email]> wrote:
>
> In my experience those situations end up been handled by collections, detect:ifNone: which even handles the case when all defaults are nil.
>
> But maybe that idiom is very common in another situations.
>
>
>
> --
> Damien Pollet
> type less, do more [ | ] http://people.untyped.org/damien.pollet


Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

Aliaksei Syrel
Check for the nil value should be banned in my opinion. There are well-known techniques to not have to use it (e.g., Null-object pattern).
Absolutely agree! :) 

 My only fear is that it could encourage the use of nil instead of proper Null Objects
Sometimes it's impossible to always have Null Object in first place and we still have to do something like this:

default1 ifNil: [ "Null object" ].

I even saw in many places

values
  ^ values ifNil: [ OrderedCollection new ]

And it perfectly works. However, usage of blocks for such simple checks looks like overhead, isn't it? (and for modern kids it looks ugly too).
Take a look how many languages already support ??:
C#, Swift, PHP7 (yes, these php-coders had to wait until 7th version), JavaScript, Groovy (they name it Evil Operator), Objective-C.

But yes, nils are evil :)

Blocks are of course better, because they are lazy and don't get executed if value is not nil - complete opposite to null coalescing operator.
 
Cheers,
Alex

On Wed, May 20, 2015 at 9:41 PM, Sven Van Caekenberghe <[hidden email]> wrote:
Yes, #ifNil: is almost the same (except the #value):

|default1 default2 default3 default4 |
default1 := nil.
default2 := nil.
default3 := 'foo'.
default4 := 'don''t care'.
default1 ifNil: [ default2 ifNil: [ default3 ifNil: [ default4 ] ] ]

=> 'foo'

> On 20 May 2015, at 21:38, Damien Pollet <[hidden email]> wrote:
>
> …ifNone: is a nice idiom, but not always available (in which case, we should think about introducing it)
>
> ?? is basically an infix but eager version of #ifNil:, IMHO a pragmatic thing to have; could be quite handy.
> My only fear is that it could encourage the use of nil instead of proper Null Objects… and here we start bikeshedding
>
> On 20 May 2015 at 21:31, Sergio Fedi <[hidden email]> wrote:
>
> In my experience those situations end up been handled by collections, detect:ifNone: which even handles the case when all defaults are nil.
>
> But maybe that idiom is very common in another situations.
>
>
>
> --
> Damien Pollet
> type less, do more [ | ] http://people.untyped.org/damien.pollet



cbc
Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

cbc

On Wed, May 20, 2015 at 12:55 PM, Aliaksei Syrel <[hidden email]> wrote:
Check for the nil value should be banned in my opinion. There are well-known techniques to not have to use it (e.g., Null-object pattern).
Absolutely agree! :) 

 My only fear is that it could encourage the use of nil instead of proper Null Objects
Sometimes it's impossible to always have Null Object in first place and we still have to do something like this:

default1 ifNil: [ "Null object" ].

I even saw in many places

values
  ^ values ifNil: [ OrderedCollection new ]

And it perfectly works. However, usage of blocks for such simple checks looks like overhead, isn't it?
<snip>

Block may be overkill - but an infix, at least in classic smalltalk, means that both sides of the operator get's evaluated.

So, 
values
   ^ values && OrderedCollection new

would generate a new OrderedCollection every time it is called!.  Even more overkill.

Or, if you use it for lazy initialization like:
values
   ^ values && (values := OrderedCollection new)
you would keep re-initializing values.

Try loading those messages and then doing:

a := Array with: 'Original Object'  " ==> #('Original Object') "
a && (OrderedCollection new)  " ==> #('Original Object') "
a  " ==> an OrderedCollection() "

Without more extensive changes than just adding  this, we'd be setting up users for a huge amount of trouble in the future.

-cbc
Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

Aliaksei Syrel
On Wed, May 20, 2015 at 10:14 PM, Chris Cunningham <[hidden email]> wrote:
would generate a new OrderedCollection every time it is called!.  Even more overkill.

Last line from previous email:
Blocks are of course better, because they are lazy and don't get executed if value is not nil - complete opposite to null coalescing operator.

That is the price that should be payed.

Cheers,
Alex

Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

Aliaksei Syrel
Yes, let's not introduce it :)
Not a smalltalk way!

Cheers,
Alex

On Wed, May 20, 2015 at 10:27 PM, Aliaksei Syrel <[hidden email]> wrote:
On Wed, May 20, 2015 at 10:14 PM, Chris Cunningham <[hidden email]> wrote:
would generate a new OrderedCollection every time it is called!.  Even more overkill.

Last line from previous email:
Blocks are of course better, because they are lazy and don't get executed if value is not nil - complete opposite to null coalescing operator.

That is the price that should be payed.

Cheers,
Alex


Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

Eliot Miranda-2
In reply to this post by Aliaksei Syrel
Hi Aliaksei,

On Wed, May 20, 2015 at 12:55 PM, Aliaksei Syrel <[hidden email]> wrote:
Check for the nil value should be banned in my opinion. There are well-known techniques to not have to use it (e.g., Null-object pattern).
Absolutely agree! :) 

 My only fear is that it could encourage the use of nil instead of proper Null Objects
Sometimes it's impossible to always have Null Object in first place and we still have to do something like this:

default1 ifNil: [ "Null object" ].

I even saw in many places

values
  ^ values ifNil: [ OrderedCollection new ]

And it perfectly works. However, usage of blocks for such simple checks looks like overhead, isn't it? (and for modern kids it looks ugly too).
Take a look how many languages already support ??:
C#, Swift, PHP7 (yes, these php-coders had to wait until 7th version), JavaScript, Groovy (they name it Evil Operator), Objective-C.

But yes, nils are evil :)

Blocks are of course better, because they are lazy and don't get executed if value is not nil - complete opposite to null coalescing operator.

ifNil:, ifNotNil:, ifNotNil:ifNil: and ifNil:ifNotNil: all get inlines as per ifTrue: et al, so no blocks are created  The only overhead is syntactic.
 
 
Cheers,
Alex

On Wed, May 20, 2015 at 9:41 PM, Sven Van Caekenberghe <[hidden email]> wrote:
Yes, #ifNil: is almost the same (except the #value):

|default1 default2 default3 default4 |
default1 := nil.
default2 := nil.
default3 := 'foo'.
default4 := 'don''t care'.
default1 ifNil: [ default2 ifNil: [ default3 ifNil: [ default4 ] ] ]

=> 'foo'

> On 20 May 2015, at 21:38, Damien Pollet <[hidden email]> wrote:
>
> …ifNone: is a nice idiom, but not always available (in which case, we should think about introducing it)
>
> ?? is basically an infix but eager version of #ifNil:, IMHO a pragmatic thing to have; could be quite handy.
> My only fear is that it could encourage the use of nil instead of proper Null Objects… and here we start bikeshedding
>
> On 20 May 2015 at 21:31, Sergio Fedi <[hidden email]> wrote:
>
> In my experience those situations end up been handled by collections, detect:ifNone: which even handles the case when all defaults are nil.
>
> But maybe that idiom is very common in another situations.
>
>
>
> --
> Damien Pollet
> type less, do more [ | ] http://people.untyped.org/damien.pollet






--
best,
Eliot
Reply | Threaded
Open this post in threaded view
|

Re: Null Coalesce Operator?

Richard Sargent
Administrator
In reply to this post by abergel
abergel wrote
Hi!

I perfectly understand the purpose of this method "??”
Although I do not think this will seriously impact the quality of the code being produced, ...
Actually, I would argue it that it would degrade the quality of code. There is less clarity conveyed by infix operators than by keyword messages, unless the infix operator is already well known (such as common arithmetic operators) or somewhat easily guessed (such as #~=). #?? only has meaning if you already know it from other languages.


... I still believe it will not help making code of a better quality. Check for the nil value should be banned in my opinion. There are well-known techniques to not have to use it (e.g., Null-object pattern).

Cheers,
Alexandre

--
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel  http://www.bergel.eu
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.



> On May 20, 2015, at 3:19 PM, Aliaksei Syrel <[hidden email]> wrote:
>
> Hi,
>
> Null coalesce operator - returns the first not nil operand.
> http://en.wikipedia.org/wiki/Null_coalescing_operator <http://en.wikipedia.org/wiki/Null_coalescing_operator>
>
> Adding one simple method to an Object:
>
> ?? anObject
>  ^ self
>
> and to UndefinedObject:
>
> ?? anObject
>  ^ anObject
>
> could allow us to write:
>  
> default1 := nil.
> default2 := nil.
> default3 := 'foo'.
> default4 := 'don''t care'.
> config := default1 ?? default2 ?? default3 ?? default4.
>
> => 'foo'
>
> I know you don't like to add new methods to an object, but still :)
>