GameMonkey Script

GameMonkey Script Forums
It is currently Tue Jun 18, 2019 2:39 pm

All times are UTC

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: null coalesce operator
PostPosted: Fri Mar 28, 2014 2:37 am 

Joined: Thu Mar 27, 2008 5:24 am
Posts: 1
Hi, just wanted to upload a patch for a null-coalesce operator I added to our local gm build recently. There are two patches, one for the ?? operator, and one for the ??= operator. The implementation in in the gmMachine is quite simple and performant.

The semantics are identical to the C# operator, which you can read about here:

The logic is basically 'a ?? b' is the same as 'if a is null b, else a'. It's useful for default values for parameters that can be null, etc.

local x = table();
local y = null;

local a = x ?? 1; // a is x because x is not null
local b = y ?? 1; // b is 1 because y is null

x ??= 1; // x is table() because it was not null
y ??= 1; // y is 1 because it was null

local shoot = function( target, damage, knockback )
  assert( ?target ); // required param
  damage ??= 1;
  knockback ??= false;

  // ... nice code that doesnt need to branch a lot ...

Attachments: [30.66 KiB]
Downloaded 277 times
Reply with quote  
PostPosted: Sat Apr 05, 2014 2:41 am 

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
Very cool kalin! Thanks for sharing, and welcome to the forum.
Feel free to reproduce entire files so people can browse, diff & merge with their own code easily.

Reply with quote  
PostPosted: Tue Apr 08, 2014 6:42 pm 

Joined: Thu Jan 01, 2004 4:31 pm
Posts: 307
Hey, nice addition. I use this operator in c# all the time and I think it's super useful.

I'll look at integrating your patch into an unofficial fork of GM on github:

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group