Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deconstructors and nullable reference types

I have a class with the deconstructor:

public void Deconstruct(out bool isSuccess, out TSuccess? value, out Error? error) {...}

Value of isSuccess defines whish of value or error is null.

Can I somehow let compiler know about this, so when I call:

var (isSuccess, value, error) = result;

analyzer knows which variable can and cannot be null?

like image 465
Pavel Voronin Avatar asked Oct 14 '25 04:10

Pavel Voronin


1 Answers

According to my knowledge you can't.

The NotNullWhenAttribute postcondition relies on the boolean return value. For example:

bool TryExecute(
    [NotNullWhen(true)] out TSuccess? value, 
    [NotNullWhen(false)] out Error? error)

Since deconstructor must be defined in the way that its return type is void, you can't mix the two techniques.

like image 124
Peter Csala Avatar answered Oct 16 '25 18:10

Peter Csala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!