Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deconstruction Nullable Tuple?

I am using C# 8.0 with NullableContextOptions (Nullable Reference) enabled.

I have a function with this signature:

public static (int line, string content)? GetNextNonEmptyLineDown(this IList<string> contents, int fromLine)

Basically, it returns the line and the content if there is non-empty line down, and returns if there is none.

The problem is I don't know how to deconstruct it. If I try:

var (firstLineIndex, firstLine) = content.GetNextNonEmptyLineDown(0);

I receive the 4 syntax errors:

enter image description here

So I can only use:

        var lineInfo = content.GetNextNonEmptyLineDown(0);

        var firstLineIndex =  lineInfo.Value.line;
        var firstLine = lineInfo.Value.content;

which destroys the purpose. lineInfo is of type struct<T> where T is (int line, string content)

Is there anyway to deconstruct a nullable tuple?

EDIT: after posting the question, it comes to my mind that it makes no sense to allow deconstructing of nullable tuple as the data type of the variables may not be determined. Here is my current fix but wonder if this is the best way:

        var lineInfo = content.GetNextNonEmptyLineDown(0);

        if (lineInfo == null)
        {
            throw new Exception();
        } 

        var (firstLineIndex, firstLine) = lineInfo.Value;
like image 840
Luke Vo Avatar asked Mar 04 '23 10:03

Luke Vo


1 Answers

From this question on nullable types:

Any Nullable is implicitly convertible to its T, PROVIDED that the entire expression being evaluated can never result in a null assignment to a ValueType.

So what you're looking for is to place at the right of the deconstruction a surely not-null expression. An elegant way to do this is:

var (firstLineIndex, firstLine) = lineinfo ?? default;

?? is the null-coalescing operator: it returns the value at its left if it is not null, the one at its right otherwise. What we place at the right is the default operator, which nicely fits its return type to the surrounding expression.

Note that the use of default is mainly to please the compiler, you may not want that value to be actually used at runtime. You should still check for null beforehand.

like image 60
rzippo Avatar answered Mar 16 '23 23:03

rzippo