GNOME Bugzilla – Bug 580816
Request coallesce operator ( ?? )
Last modified: 2010-01-29 18:29:51 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!
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? ;)
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)
Created attachment 152373 [details] [review] Implement coallesce operator ( ?? ) Fixes bug 580816.
Comment on attachment 152373 [details] [review] Implement coallesce operator ( ?? ) I prepare a better patch, this one has several issues.
Created attachment 152451 [details] [review] Implement coallesce operator ( ?? ) Fixes bug 580816.
Created attachment 152452 [details] [review] Implement coallesce operator ( ?? ) Fixes bug 580816.
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.
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.