Replies: 5 comments 1 reply
-
|
I think that's more an argument for allowing arbitrary types to support |
Beta Was this translation helpful? Give feedback.
-
I'd be content with that, but it seems like a bigger change, whereas a |
Beta Was this translation helpful? Give feedback.
-
Question 1 Not compiling is exactly my preferred preference given I know where to 'fix' or change the code - that does not work anymore. Given int? a = null;
Console.WriteLine(a ?? 1);
// and some other part of the code might do:
Console.WriteLine(a ?? 2);As I understand switching to the suggested NotNullable the code would still compile but it would be a breaking change in the behavior, both console writes printing 0. Question 2 I am not sure how this would help you in other parts of the application either. Given existing method parameters that gets a value from a source that changed from
|
Beta Was this translation helpful? Give feedback.
-
|
Question 1. That would not compile as you cannot assign Question 2. Sorry, I omitted to make clear that |
Beta Was this translation helpful? Give feedback.
-
|
This is not something we're interested in pursuing. It is a binary break to change from
As for needing to fixup many places at the same time when you change a type like that, it really sounds like something that people intentionally desire and/or which could be solved via tooling (such as an analyzer). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Sometimes you change a type from nullable to not-nullable. For example from
int?toint. But any code that checks the value against null, or uses the?conditional access operator or??defaulting operator, will no longer compile. You must change all those occurrences at the same time as tightening the type.Sometimes it would be useful to make the change in two steps. I propose
NotNullable<int>which is a trivial wrapper for the underlying typeint. It supportsHasValuewhich always returns false, andValuewhich always returns the value. Comparison againstnull, the??operator, and?.conditional access work too. At the same time it can be implicitly converted to a plainintwithout problems. You could change the type toNotNullable<int>without altering any other code, but then go through and simplify out the null tests later (perhaps helped by your development environment or by a compiler warning). In a large codebase with many branches, breaking up a change like this helps avoid merge conflicts and programmers blocking each other.Another motivation is for automatically generated wrapper classes. For example some tools generate a class definition from a table definition in an SQL database. When the database has the type
int nullit will generateNullable<int>in C#, and when the database hasint not nullit generates justint. However, if the database is changed to add a not null constraint, most C# code using the wrapper class will no longer compile. On the SQL side, changing a column fromnulltonot nullkeeps allselectqueries working, and is effectively making a stronger promise on the values of the column. But the generated wrapper code in C# cannot stay working with the more constrained type, even if it is giving read-only access.By having
NotNullable<T>available in the language, the wrapper code for anint not nullwould beNotNullable<int>. Any code written against the previous database schema that only provided anint null(wrapped as aNullable<int>) would continue to build and work correctly, although as before, you could later go through it and remove the redundant null checks.I have filed this as a language idea as I'm not sure whether the
NotNullableclass could be implemented in the standard library. If it could, and still perform as well as a plain unwrapped type, please recategorize this as a standard library request.Beta Was this translation helpful? Give feedback.
All reactions