GameMonkey Script

GameMonkey Script Forums
It is currently Tue Oct 23, 2018 3:51 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 
Offline

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: http://msdn.microsoft.com/en-us/library/ms173224.aspx.

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.

Code:
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:
gm-null-coalesce-patch.zip [30.66 KiB]
Downloaded 150 times
Top
 Profile  
Reply with quote  
PostPosted: Sat Apr 05, 2014 2:41 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 707
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.


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

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: https://github.com/evolutional/gamemonkey-unofficial


Top
 Profile  
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 1 guest


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:  
cron
Powered by phpBB® Forum Software © phpBB Group