www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Nullable condition operator

reply Martin6265 <marin6265 gmail.com> writes:
Hello,
I think nullable condition operator and null coalescending 
operator should be a nice new features in D.

https://msdn.microsoft.com/library/dn986595.aspx
https://msdn.microsoft.com/library/ms173224.aspx

Same as operator `as`
https://msdn.microsoft.com/library/cscsdfbt.aspx

Why?
Is better to write
(variable as FooBar)?.callMethod();

than
auto var = cast(FooBar)variable;
if (var !is null)
var.callMethod();


or
auto x = (variable as FooBar)?.getRandomNumber() ?? 42;
Dec 07 2015
next sibling parent reply rumbu <rumbu rumbu.ro> writes:
On Monday, 7 December 2015 at 11:40:45 UTC, Martin6265 wrote:
 Hello,
 I think nullable condition operator and null coalescending 
 operator should be a nice new features in D.
Happy reading: http://forum.dlang.org/post/lnsc0c$1sip$1 digitalmars.com
Dec 07 2015
parent BBaz <b2.temp gmx.com> writes:
On Monday, 7 December 2015 at 13:28:21 UTC, rumbu wrote:
 On Monday, 7 December 2015 at 11:40:45 UTC, Martin6265 wrote:
 Hello,
 I think nullable condition operator and null coalescending 
 operator should be a nice new features in D.
Happy reading: http://forum.dlang.org/post/lnsc0c$1sip$1 digitalmars.com
Why the template hasn't been integrated in Phobos ? It seems that there was a kind of strong agreement, but one year later still nothing. It's like the list template that allows to assign MRV.
Dec 08 2015
prev sibling next sibling parent reply Meta <jared771 gmail.com> writes:
On Monday, 7 December 2015 at 11:40:45 UTC, Martin6265 wrote:
 Hello,
 I think nullable condition operator and null coalescending 
 operator should be a nice new features in D.

 https://msdn.microsoft.com/library/dn986595.aspx
 https://msdn.microsoft.com/library/ms173224.aspx

 Same as operator `as`
 https://msdn.microsoft.com/library/cscsdfbt.aspx

 Why?
 Is better to write
 (variable as FooBar)?.callMethod();

 than
 auto var = cast(FooBar)variable;
 if (var !is null)
 var.callMethod();


 or
 auto x = (variable as FooBar)?.getRandomNumber() ?? 42;
As per Rumbu's link, D is powerful enough to implement this in a library function, and have it optimize down to a set of if-else statements.
Dec 07 2015
parent reply Martin6265 <Martin6265 gmail.com> writes:
On Monday, 7 December 2015 at 15:13:31 UTC, Meta wrote:
 On Monday, 7 December 2015 at 11:40:45 UTC, Martin6265 wrote:
 Hello,
 I think nullable condition operator and null coalescending 
 operator should be a nice new features in D.

 https://msdn.microsoft.com/library/dn986595.aspx
 https://msdn.microsoft.com/library/ms173224.aspx

 Same as operator `as`
 https://msdn.microsoft.com/library/cscsdfbt.aspx

 Why?
 Is better to write
 (variable as FooBar)?.callMethod();

 than
 auto var = cast(FooBar)variable;
 if (var !is null)
 var.callMethod();


 or
 auto x = (variable as FooBar)?.getRandomNumber() ?? 42;
As per Rumbu's link, D is powerful enough to implement this in a library function, and have it optimize down to a set of if-else statements.
But the syntax of it will be like calling template func, and its not desirable. The trick is in the simplicity of the pattern not only in functionality. Look, Im developing multiplatform GUI framework (based on .NET conventions and Cocoa) & ID. I can make my custom version of D (anyway, Ill distribute it as a one package product) but Im not happy to make new, uncompatibile version.
Dec 11 2015
next sibling parent ZombineDev <valid_email he.re> writes:
On Friday, 11 December 2015 at 08:30:21 UTC, Martin6265 wrote:
 But the syntax of it will be like calling template func, and 
 its not desirable.
 The trick is in the simplicity of the pattern not only in 
 functionality.
No, by using UFCS and IFTI, the syntax of `maybe` becomes even // http://dlang.org/glossary.html // UFCS: https://dlang.org/spec/function.html#pseudo-member // IFTI: https://dlang.org/spec/template.html#variadic-templates (search for "implicit") struct Node { int val; Node* left, right; } void main() { import std.stdio; auto tree = new Node(1, new Node(2), new Node(3, null, new Node(4) ) ); writeln(tree.maybe.right.right.val); writeln(tree.maybe.left.right.left.right); writeln(tree.maybe.left.right.left.right.val); } // Full source: http://dpaste.dzfl.pl/8b5dcec7aaf3
 Look, Im developing multiplatform GUI framework (based on .NET 
 conventions and Cocoa) & ID.
I'm interested to know more about the multiplatform GUI framework you are developing. What are the main features, goals and design ideas, architecture and programming model?
 I can make my custom version of D (anyway, Ill distribute it as 
 a one package product) but Im not happy to make new, 
 uncompatibile version.
What do you miss the most in D? What would you change to be worthwhile the effort to distribute a custom version of D? better in some stuff than D. However now I would never go back to
Dec 11 2015
prev sibling parent Meta <jared771 gmail.com> writes:
On Friday, 11 December 2015 at 08:30:21 UTC, Martin6265 wrote:
 But the syntax of it will be like calling template func, and 
 its not desirable.
 The trick is in the simplicity of the pattern not only in 
 functionality.
I guess it's somewhat subjective, as while I liked the ?. syntax at first I now find it very noisy. You?.have?()?.a?.million?.question?.marks?()?.cluttering?.your?.code I find the D syntax much cleaner, because it's just regular function call syntax. maybe(You).have().exactly.zero.question.marks().cluttering.your.code
Dec 11 2015
prev sibling parent reply Martin6265 <marin6265 gmail.com> writes:

other languages.

send message to invalid object without accessing null pointer). 
And I miss that stuff in D.

Maybe will create a new instance of T if T is null, right?

class Responder {
Responder m_parent;


void SetSomething(int a) {
m_parent.maybe.SetSomething(a);
}
}

So, this example will ends in infinite loop.
But I don't want to call SetSomething on m_parent if m_parent is 
null.
Dec 14 2015
next sibling parent ZombineDev <valid_email he.re> writes:
On Monday, 14 December 2015 at 13:19:13 UTC, Martin6265 wrote:
 Maybe will create a new instance of T if T is null, right?
If you look at the code, you will see that it would not.
Dec 14 2015
prev sibling parent rumbu <rumbu rumbu.ro> writes:
On Monday, 14 December 2015 at 13:19:13 UTC, Martin6265 wrote:

 from other languages.

 send message to invalid object without accessing null pointer). 
 And I miss that stuff in D.

 Maybe will create a new instance of T if T is null, right?

 class Responder {
 Responder m_parent;


 void SetSomething(int a) {
 m_parent.maybe.SetSomething(a);
 }
 }

 So, this example will ends in infinite loop.
 But I don't want to call SetSomething on m_parent if m_parent 
 is null.
Despite the enthusiasm in the aforementioned topic and despite the obsession that "every other language feature can be transformed in a D library solution beacuse D is so powerful", the D monad-like implementation is incomplete and is not result type. Compiling your example, will result in an error message - "no field SetSomething for Maybe" (or similar). The D implementation will not create a new instance - it checks for typeof(null) - it will return typeof(Responder.SetSomething).init if SetSomething would be a field (which is not). My advice is to stick to conditional code for now, not every other language feature can be translated. A single suggestion your example code:
 auto var = cast(FooBar)variable;
 if (var !is null)
 var.callMethod();
can be written as: if (auto var = cast(Foobar)) var.callMethod();
Dec 14 2015