Questions tagged [non-nullable]
232 questions
5
votes
1 answer
How to initialize non-nullable strings in a class?
I have this project:
Exe
netcoreapp3.0
enable

pavok
- 380
- 3
- 10
5
votes
2 answers
C#8 what does "default!" do on generic types?
Just playing around with C# 8.0 Beta and updating some of my code to use nullable reference types.
I have a node style class for a Trie implementation. Each node has a value of type T. The constructor for the root node does not need a value, so I…

craig
- 421
- 4
- 13
5
votes
3 answers
Having a getter return a non-nullable type even though the backing field is nullable
num should be nullable when set, but what it returns should always be non-nullable (have a default value).
class Test {
var num: Int? = null
get() = field ?: 5 // default value if null
}
The following does not compile even though the…

Willi Mentzel
- 27,862
- 20
- 113
- 121
5
votes
2 answers
No warning when using nonnull?
With code like this:
- (nonnull NSString *)testing {
return nil;
}
Shouldn't I get a compiler warning? I get no warning all, which seems to make the entire nullability stuff seem useless?

Kevin Renskers
- 5,156
- 4
- 47
- 95
5
votes
1 answer
WCF Data Contract - best/cleanest way to enforce required values?
I have a WCF data contract with a bunch of properties of primitive types like int and decimal, and DateTime (which is, of course, a struct).
My co-worker suggested making them all nullable and then validating required values on the service end by…

lintmouse
- 5,079
- 8
- 38
- 54
4
votes
2 answers
C# 10 - is there a way to make "var" define the variable as non-nullable?
When I write this statement:
var x = new ClassName();
x is implicitly typed as a ClassName?. That is, since ClassName is a reference type, implicit assignments using var automatically define as nullable (even when I am providing a non-null instance…

Overlord Zurg
- 3,430
- 2
- 22
- 27
4
votes
4 answers
how to tell compiler that property MUST be initialized
I have a class like e.g. this:
public class Foo
{
public string Bar { get; init; }
public IImmutableSet Baz { get; init; }
}
When I write it that way, I get a compiler warning
Non-nullable property 'Bar' must contain a non-null value…

Kjara
- 2,504
- 15
- 42
4
votes
1 answer
Why is "default" different from "null" when assigning to a nullable type constrained by notnull?
Consider this minimal-ish example:
class A where T : notnull
{
public T? Item; // OK!
public T? Something() => default; // OK!
public void Meow(ref T? thing1, out T? thing2) { thing2 = default; } // OK!
void Something(A?…

Josh
- 2,958
- 3
- 16
- 27
4
votes
1 answer
Freezed package flutter throwing non-nullable error in code generation
I am starting a project and decided to use the new version version of **Freezed** for my models, but when I run flutter *pub run build_runner build* to generate my code I get the following error:
>The parameter 'placeFormattedAddress' of 'Address'…

sempernoob
- 186
- 2
- 8
4
votes
4 answers
Is there a standard Java NonNullable generic, and if not, why not?
The problem with annotations such as @NotNull/@NonNull is that they aren't enforced at runtime. I want to do something like the following:
class Foo {
String nullable_string;
final NonNullable nn_string = new NonNullable();
…

Jeremy
- 41
- 2
4
votes
3 answers
How to make Object property of a C# class optional?
I am trying to make a model class in C# in which i require object/List properties as optional property:
public class Customer
{
[JsonProperty("Custid")]
public string CustId { get; set; }
[JsonProperty("CustName")]
…

tripti sinha
- 149
- 1
- 1
- 6
4
votes
1 answer
Is it somehow possible to create a collection of Foo objects where T is restricted to a non-nullable type?
Inside of a class I have the following struct
private struct StateGroup where TState : struct, IAgentState
{
// ...
// ComponentDataArray requires TState to be a struct as well!
public ComponentDataArray AgentStates;
…

Christian Ivicevic
- 10,071
- 7
- 39
- 74
4
votes
1 answer
Is it valid to use ptr::NonNull in FFI?
Rust has the ptr::NonNull type that represents a non-NULL pointer. Is it safe to use this type in FFI?
Is it guaranteed to have same binary representation (ignoring non-FFI context such as Option optimizations), alignment, register usage as *mut…

Kornel
- 97,764
- 37
- 219
- 309
4
votes
1 answer
In Kotlin, how do I idiomatically access nullable nested map values, or return a default?
Quick Kotlin best practices question, as I couldn't really work out the best way to do this from the documentation.
Assume I have the following nested map (typing specified explicitly for the purpose of this question):
val userWidgetCount:…

Rik
- 790
- 8
- 11
4
votes
3 answers
Non-null and non-undefined type in Flow
Flow defines so called "Maybe types". I.e. ?string is similar to string | null | void (void is a type of value undefined).
Is there something like general type that can be of any value but null and undefined? Basically something like…

czerny
- 15,090
- 14
- 68
- 96