#1129 ?= operator to assign a value if null

tcolar Mon 28 Jun 2010

I keep running into case where I think an operator ?= would be cool.

For example: var ?= 25 // means if(var==null) var = 25

I know there is already var = var ?: 25, but that's a bit longer, and also the : in ?: seem to suggest initial assignment only, though it's more of a ternary shorthand I guess.

brian Fri 2 Jul 2010

I don't think I personally have run across that case very often.

Although I do find that I have a ton of code like this:

if (x == null) ...
if (x != null) ...

But I haven't put my finger on the pattern that could really lead to a nice shortcut for code like that.

DanielFath Sun 4 Jul 2010

So basically we have

x?.bar
x?->bar
x?:bar
//And to add 
x?=bar

So basically x?* would mean if x is null then do * thing otherwise don't. How would this syntax fit?

x?  {//do something if x not null}   
x!? {//do something if x is null}

Disclaimer: I don't yet grasp the functional programming in its entirety, but here goes. Make a slot in Obj

sys.Obj

boolean doesExist(|Func f:=null|) 
//boolean doesNotExist(|Func f:=null|) 

and make x? and x!? shortcut to doesExist and doesNotExist respectivelly.

This only solves problem for non-operator stuff. x?=3 and other would be added if you really need them.

brian Mon 5 Jul 2010

That is kind of an interesting idea - to make a ? {...} syntax.

What do other people think of that?

rfeldman Mon 5 Jul 2010

On the one hand, I'm intrigued by the novelty, on the other hand I'm concerned about code readability.

I think foo ?= 25 is decently self-documenting, but if I came across this in someone else's code:

foo!?
{
  // Do some logic here...
}

I'm not sure I'd know what to make of it. (Except that I'd happened to have read this thread; certainly that would never be front-and-center in the language docs.) And that's not syntax that is easy to Google if you're confused by it. Seems like kind of a high price to pay to save a few characters.

ivan Mon 5 Jul 2010

For me foo? { } looks very consistent, since it is similar to safe implicit call of sys::Obj.with

qualidafial Mon 5 Jul 2010

I'm for it, as long as it doesn't conflict with the ternary operator.

One other thing I've thought would be nice is to use ? as a non-null test operator all by itself:

if (foo?)
{
  // do stuff
}

It's a little more complex on the compiler side to distinguish between that and the ternary syntax, but is a lot more aesthetically pleasing than if (foo != null).

Up to Brian and Andy if it's worth the extra work but personally I'd really like this feature.

brian Mon 5 Jul 2010

Quick grep of our skyspark code base turns up 291 occurrences of if (xxx == null) and 306 occurrences of if (xxx != null).

Does that warrant syntax sugar? I can see both sides of the fence since we are essentially taking about 6 chars here. I think whatever the syntax looks like it would need to support both == null and != null since they seem to be about 50/50.

liamstask Mon 5 Jul 2010

I would conservatively say leave it out - it's yet another thing to implement, maintain, document, etc and doesn't (IMO) provide a huge benefit in terms of clarity or brevity.

DanielFath Mon 5 Jul 2010

Well I just made foo!? as example but foo! could work as well, I think. Also

if (foo?){
}

is still valid syntax in my example, since the doesExist returns boolean.On the other hand, you could write something like

if (foo?
    {
      foo = foo.bar
    }) 
{
  //do more something
  foo = foo.more.bar
}

I dunno, there was a problem and I saw a more or less elegant solution. Andy and Brian are the devs and as often goes devs have to filter this stuff.

jodastephen Tue 6 Jul 2010

Number of suggestions from others and elsewhere:

if (foo != null) { ... }   // current
foo? { ... }               // no if
if (foo?) { ... }          // query null operator
if (foo) { ... }           // object alone

if (foo == null) { ... }   // current
foo!? { ... }              // no if
if (foo!?) { ... }         // query not-null operator option A
if (foo!) { ... }          // query not-null operator option B
ifnot (foo) { ... }        // object alone

// using current not operator
!foo? { ... }              // no if
if (!foo?) { ... }         // query null operator
if (!foo) { ... }          // object alone

// oddities
if (!foo!?) { ... }        // query not-null operator option A
if (!foo!) { ... }         // query not-null operator option B
ifnot (foo?) { ... }       // query null operator

Personally, I really like the ifnot keyword. I think it heaps more readable than the ! symbol at the start of the query. In fact, I always write if (x == false) rather than if (!x).

Before choosing between the rest, its important to remember that the ! "not" operator can be used before any of the expressions. As shown above, this can create some very odd expressions like if (!foo!) or if (!foo!?). I think those should be avoided.

I dislike the foo? {...} approach, unless we also support foo {...}. At that point the question mark is just a modifier to a general concept. This would then be a general purpose language feature to lift the slots of foo into a higher scope, like the VB with statement. Overall though, I'm not sure I like that direction.

Therefore, I prefer the "object alone" approach (with if and ifnot). Rather than making it generally OK to assign an object to a Boolean, it should only be allowed in if (foo) and ifnot (foo). Ideally this would be like Groovy Truth. Also, I'd argue that if (!foo) and ifnot (!foo) should not be supported.

yachris Tue 6 Jul 2010

On "ifnot"... I guess I'd prefer "unless", which is used in Ruby.

andy Tue 6 Jul 2010

On "ifnot"... I guess I'd prefer "unless", which is used in Ruby.

As a reference - older discussion on ifnot: #276

rfeldman Tue 6 Jul 2010

Quick note: if (foo) cannot be used to test nullability because it is ambiguous in the case of a Bool? - it will return true if the object is null or if it evaluates to true.

tcolar Tue 6 Jul 2010

The one thing I don't like about this is that

Button? {text="dfds"} // It block on nullable button - is that allowed ?

and

button? {text="dfds"} // proposed "if button is not null" syntax

Are syntactically the same, and will be painful to parse.

brian Tue 6 Jul 2010

I always think these discussions are useful now and then, especially given that checking for null and non-null are such common patterns. But probably best to just sit on this and not add anything to the language yet.

yachris Wed 7 Jul 2010

Very wise. I think the "Boring by design" article points towards what's so cool about Fantom: it's concise. Java (which I love) has gotten SO bloated... and now that I'm doing C++, WOW, really, PLEASE look at what they're adding in C++0x and DO THE OPPOSITE! :-)

It's just so cool to see things pared away from the "standard" ideas of what a language should be; true, things can be too concise, but I vote to err on the side of fewer "features" than on the side of too many.

Login or Signup to reply.