Allow opt in to usage of ?. and ?? for default structs #2243
Replies: 21 comments
-
This is definitely not true. Even ignoring members like |
Beta Was this translation helpful? Give feedback.
-
I like this idea. |
Beta Was this translation helpful? Give feedback.
-
Its the same isn't it? class C
{
public ImmutableArray<string> Strings { get; set; }
static void Main(string[] args)
{
C c = null;
var str = c?.Strings.FirstOrDefault();
Console.WriteLine(str == null); // outputs true
}
} |
Beta Was this translation helpful? Give feedback.
-
Oh less one var str = c?.Strings?.FirstOrDefault(); // string[] To var str = c?.Strings.FirstOrDefault(); // ImmutableArray<string> |
Beta Was this translation helpful? Give feedback.
-
That's true. But |
Beta Was this translation helpful? Give feedback.
-
Ah, gotcha static void Main(string[] args)
{
C c = null;
var str = c?.Strings.FirstOrDefault();
Console.WriteLine(str == null); // true
c = new C();
str = c?.Strings.FirstOrDefault(); // null ref
Console.WriteLine(str == null);
} Surely a bug in |
Beta Was this translation helpful? Give feedback.
-
Nope. It's by design. A default ImmutableArray throws when you try and enumerate it. |
Beta Was this translation helpful? Give feedback.
-
That's just throwing half the advantage of it being a struct away (don't worry about null) |
Beta Was this translation helpful? Give feedback.
-
ImmutableArray isn't a struct so that it can be non-null. It's a struct so that it can enforce, with nearly no overhead (either memory or CPU), that the array is actually immutable. It's meant to represent what an array can represent. And arrays can be null, and so ImmutableArray needs to be able to represent htat as well (without costing anything). |
Beta Was this translation helpful? Give feedback.
-
I had this problem as well. the only reliable solution is to use IsDefault. It's weird decision because they made a dedicated set of Linq methods for ImmutableArray in One solution would be
|
Beta Was this translation helpful? Give feedback.
-
#147 ? |
Beta Was this translation helpful? Give feedback.
-
@ufcpp |
Beta Was this translation helpful? Give feedback.
-
There are a handful of places that I would not enforce memberwise comparisons, though -- this will be wasteful because largely you will only be inspecting a single member rather than all members. For this reason I'd support the idea of patterns, allowing the "nullness" check to be optimized according to the type. |
Beta Was this translation helpful? Give feedback.
-
#2020 Proposal: Allow null conditional (?.) and null coalescing ()?? operators to be overloaded Although it seems all roads lead to #147. |
Beta Was this translation helpful? Give feedback.
-
@GalaxiaGuy #147 is most similar, but a bit over engineered IMO. |
Beta Was this translation helpful? Give feedback.
-
#146 ? |
Beta Was this translation helpful? Give feedback.
-
I suggested something similar to that as well: #2019 The issue is the boat has already sailed on this one. Its too late to make ImmutableArray non-defaultable. Also this should be significantly cheaper to implement. |
Beta Was this translation helpful? Give feedback.
-
Roles could solve this issue? |
Beta Was this translation helpful? Give feedback.
-
@ufcpp |
Beta Was this translation helpful? Give feedback.
-
ImmutableArray<int> a = (int[])null;
object obj = a;
|
Beta Was this translation helpful? Give feedback.
-
A role would not be truly immutable as you could change the underlying array. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm currently converting some code from using Arrays to ImmutableArrays, and one thing that is really bothering me is the poor story around default ImmutableArrays.
A default ImmutableArray is for all intents and purposes null - any attempt to access a member will throw a NullReferenceException. However, it's not technically null, and so all the language tools built around nullability, such as the ?? and ?. operators don't work. Instead you have to check IsDefault.
This can often lead to a serious uglification of code. For example, consider how this code originally looked using arrays:
And how it looks once Strings is converted to an ImmutableArray:
Significantly noisier.
Multiply that by a hundred different locations, and a hundred unnecessary temporary variables added, and I'm seriously starting to wonder whether the benefit of using immutable types is worth the loss in clarity that ensues.
Proposed Solution
Allow indicating that default for a struct should be treated as a pseudo-null case.
This could be done by an attribute, a new keyword, or a pattern such as an IsDefault property.
When this is done, ?? and ?. should be valid on such a struct. ?? becomes the default coalescing operator, and ?. remains the safe navigation operator, but checks for default not just null.
This could be done either by using .Equals, or by generating a method that checks if the struct is in a default state.
Then ImmutableArray could be marked as such a struct, and we can continue using ?. and ?? as before.
Beta Was this translation helpful? Give feedback.
All reactions