| Thread overview | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 07, 2015 Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
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; | ||||
December 07, 2015 Re: Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin6265 | 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 | |||
December 07, 2015 Re: Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin6265 | 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.
| |||
December 09, 2015 Re: Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
Posted in reply to rumbu | 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.
| |||
December 11, 2015 Re: Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Meta | 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.
| |||
December 11, 2015 Re: Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin6265 | 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 better than C#'s `?.`. See: // 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? Before learning about D, I worked on UI components with C#/WPF|SL so I can understand that in the beginning it seems that C# is better in some stuff than D. However now I would never go back to C# and .NET, because I would miss so much of D's features and I don't remember anything that I miss from C# and .NET. | |||
December 11, 2015 Re: Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin6265 | 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 | |||
December 14, 2015 Re: Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin6265 | I dont said C# is better, just missing some preferred stuffs from other languages.
`.?` from C# is same thing as message passing in ObjC. (You can 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.
| |||
December 14, 2015 Re: Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin6265 | 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.
| |||
December 14, 2015 Re: Nullable condition operator | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin6265 | On Monday, 14 December 2015 at 13:19:13 UTC, Martin6265 wrote: > I dont said C# is better, just missing some preferred stuffs from other languages. > `.?` from C# is same thing as message passing in ObjC. (You can 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 equivalent to the C# ? operator, it wrongly assumes a field as a 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(); | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply