Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Extend null-conditional (?., ?[]) and null-coalescing (??) operators to pointers #398

Open
YaakovDavis opened this issue Apr 1, 2017 · 5 comments
Assignees
Labels
Needs Approved Specification This issue needs an LDM-approved specification Proposal champion Smallish Feature
Milestone

Comments

@YaakovDavis
Copy link

YaakovDavis commented Apr 1, 2017

I'd like to be able to write:

Data* p = GetUnmanagedData();
p?->Act();
var a = p?->X ?? 42;
var b = p?[3] ?? new Data();
var p2 = p ?? AnotherDataPointer();

The above should compile to the equivalent of the following:

Data* p = GetUnmanagedData();
if(p != null)
   p->Act();

var a = p != null ? p->X : 42;
var b = p != null ? p[3] : new Data();
var p2 = p != null ? p : AnotherDataPointer();
@svick
Copy link
Contributor

svick commented Apr 1, 2017

This is half of a duplicate of #323.

@YaakovDavis
Copy link
Author

YaakovDavis commented Apr 1, 2017

@svick
I thought I had encountered something similar, but couldn't find it.
In any case, this proposal is more encompassing.

@YaakovDavis YaakovDavis changed the title Proposal: Extend null-conditional (?.) and null-coalescing (??) operators to pointers Proposal: Extend null-conditional (?., ?[]) and null-coalescing (??) operators to pointers Apr 1, 2017
@jacekbe
Copy link

jacekbe commented Apr 6, 2017

When I proposed #323 I completely forgot about null-conditional operators. Obviously it would be nice if both null-conditional and null-coalescing operator worked also for pointer types.

Note that null-coalescing operator is already supported by VS2017 quick actions. VS2017 hints that it could be simplified to use null-coalescing operator even though resulting code does not compile.

 unsafe byte* SomeFunc(byte* p, byte* defaultVal)
 {
      return p != null ? p : defaultVal;            
 }

@gafter
Copy link
Member

gafter commented Apr 7, 2017

I've queued this to be considered by the LDM at #418.

@gafter
Copy link
Member

gafter commented Jul 17, 2018

@lachbaer The LDM discussed this today and we decided that while it isn't something we would devote resources to implementing, we'd probably accept a contributed implementation along with #418.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Approved Specification This issue needs an LDM-approved specification Proposal champion Smallish Feature
Projects
None yet
Development

No branches or pull requests

6 participants