Extend definite assignment to understand null and nullable values and ?. #916
-
I am currently using the following IDE:
I'm have a simple .NET Core 2.0 application that I have built targeting C# 7.1 from the using System.Collections.Concurrent;
namespace IEvangelist.Csharp.Seven
{
class Program
{
public static void Main() { }
}
public static class ConcurrentExtensions
{
public static T TryPeekOrDefault<T>(this ConcurrentQueue<T> queue)
=> (queue?.TryPeek(out T result) ?? false) ? result : default;
}
} Error reported:
Expected Behavior No compiler error. The compiler has all the details it needs to know that this is valid. The |
Beta Was this translation helpful? Give feedback.
Replies: 12 comments
-
The error is correct, the |
Beta Was this translation helpful? Give feedback.
-
you can fix this by
I don't think this can be simplified further in one line. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I guess some of new c#7 does not work very well. this one is good find. you can not use similar issue where you can not use contents of local variable..
There are similar issues too not just with c#7, its just matter of writing right syntax for it. how ever maybe they could introduce
just for fun, oh does this require changes in CLR? :-) |
Beta Was this translation helpful? Give feedback.
-
C#7 has nothing to do with it. If the variable was declared in a prior statement (but not assigned) the result would be exactly the same. And that behavior is correct. If As the behavior is correct there is nothing to correct here. But, for fun, no, changing that would not require CLR changes. The CLR only has a flat method scope for variables and no concept of definite assignment. Those are strictly C# features. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour If So, there is indeed nothing to correct, but I think there is something to improve. (Whether it's worth improving is another question entirely.) |
Beta Was this translation helpful? Give feedback.
-
Maybe. That starts to wade into mixing definite assignment with flow analysis. Even though it all looks like a single expression it expands out to numerous statements and the compiler requires that both operands meet definite assignment since it can't base that assignment on whether the |
Beta Was this translation helpful? Give feedback.
-
Thank you all for the replies, clarifications and thoughts... much appreciated. I agree with @svick after revisiting this again. I do however see it as an opportunity for improvement. Flow analysis (outside of definite assignment) is starting to make its way into proposals and prototypes for C# 8. I'd love to see this improved as part of that. |
Beta Was this translation helpful? Give feedback.
-
@IEvangelist You say you'd like to see this "improved". What would you like to be better? Is the error message unclear? |
Beta Was this translation helpful? Give feedback.
-
Oh, I see you'd like definite assignment (which is part of flow analysis) to infer that the variable is only referenced where it was definitely assigned in this case. A change to accomplish that would require a change to the definite assignment spec at https://github.com/dotnet/csharplang/blob/master/spec/variables.md#definite-assignment . |
Beta Was this translation helpful? Give feedback.
-
Has there been any internal discussion on this issue? |
Beta Was this translation helpful? Give feedback.
-
Encountered this "problem" today, and I'd really love if definite assignment could be extended to work in this case. In my case I have to preassign my out value which is an enum. Using
|
Beta Was this translation helpful? Give feedback.
-
A spec proposal which addresses many of these scenarios has been submitted at #4240. |
Beta Was this translation helpful? Give feedback.
A spec proposal which addresses many of these scenarios has been submitted at #4240.