After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 580816 - Request coallesce operator ( ?? )
Request coallesce operator ( ?? )
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: general
unspecified
Other All
: Normal enhancement
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2009-04-29 22:37 UTC by IagoSRL
Modified: 2010-01-29 18:29 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Implement coallesce operator ( ?? ) (2.30 KB, patch)
2010-01-27 01:13 UTC, Marc-Andre Lureau
none Details | Review
Implement coallesce operator ( ?? ) (5.72 KB, patch)
2010-01-27 21:53 UTC, Marc-Andre Lureau
none Details | Review
Implement coallesce operator ( ?? ) (5.22 KB, patch)
2010-01-27 22:02 UTC, Marc-Andre Lureau
none Details | Review

Description IagoSRL 2009-04-29 22:37:27 UTC
In C# 2.0 exists the coallesce operator ?? used like a shortest syntax for:

 // assign to 'x' the value of 'y' but if 'y' is null assign the value of 'z'
 object x = y != null ? y : z;

Same (with coallesce operator):

 object x = y ?? z;
 // this is clearer

And I can use more 'options':

 object x = y ?? z ?? m ?? n
 // etc.
 // this means: get the value of 'y' else get 'z' else 'm' else 'n'

More info: http://msdn.microsoft.com/en-us/library/ms173224%28VS.80%29.aspx

Plus Note: Innovation

 It's not in C#, but can be useful a similar feature than coallesce operator for the special case with strings vars (or ever object autoexecuting his method 'ToString'): empty; too much times is needed know if a string var it's null Or empty (in ms.net2.0+ you can use the method called 'String.IsNullOrEmpty' for this). And if the string var is null Or empty Or only-white-space-chars (a possible method would be 'String.IsNullOrWhite' -it isn't exists in ms.net-). Link about "white-Space Characters":http://msdn.microsoft.com/en-us/library/e9a023cx.aspx
 Is it possible create news operators or shorted name functions for this two plus features??

Thanks!
Comment 1 IagoSRL 2009-04-29 22:42:23 UTC
Sorry: let me an appointment:

 In javascript for this feature (coallesce operator) people use the binary operator OR (in C-syntax: || )
 For example:

 object x = y || z;

 object x = y || z || m || n;


 What is it clearer?
 What do you like more? ;)
Comment 2 zarevucky.jiri 2009-06-25 18:37:11 UTC
Vala is largely derived from C#, so introducing JavaScript syntax isn't really very logical. I'd rather not overload boolean operator for this anyway. 
I'd really like this operator, too, and it's as simple as:
(a ?? b) <=> (a == null ? b : a) 
Comment 3 Marc-Andre Lureau 2010-01-27 01:13:57 UTC
Created attachment 152373 [details] [review]
Implement coallesce operator ( ?? )

Fixes bug 580816.
Comment 4 Marc-Andre Lureau 2010-01-27 20:03:26 UTC
Comment on attachment 152373 [details] [review]
Implement coallesce operator ( ?? )

I prepare a better patch, this one has several issues.
Comment 5 Marc-Andre Lureau 2010-01-27 21:53:21 UTC
Created attachment 152451 [details] [review]
Implement coallesce operator ( ?? )

Fixes bug 580816.
Comment 6 Marc-Andre Lureau 2010-01-27 22:02:12 UTC
Created attachment 152452 [details] [review]
Implement coallesce operator ( ?? )

Fixes bug 580816.
Comment 7 Jürg Billeter 2010-01-29 18:29:28 UTC
Your patch uses void* for the temporary variable. This leads to compile-time erros in the following example:

class Foo {
}

void main() {
    Foo foo = new Foo();
    Foo bar = null;
    Foo baz = bar ?? foo;
}

I've fixed this and also removed the redundant compatibility checks. Checking the temporary variable declaration and the if statement already performs all the type checking.
Comment 8 Jürg Billeter 2010-01-29 18:29:51 UTC
commit b319ccfbfd263417ef0724bf3346eb3765cf75a9
Author: Jürg Billeter <j@bitron.ch>
Date:   Wed Jan 27 02:13:24 2010 +0100

    Implement coalescing operator ??
    
    Based on patch by Marc-André Lureau, fixes bug 580816.