[Implemented] null-coalescing assignment operator as in C# 8.0

I’d like to see the null-coalescing assignment operator from c# 8.0 where the value is only assigened when the vatable has a null value.

Requested syntax:

MyObject ?= SomeValueOrFunction

This should translate to:

If MyObject Is Null then
    MyObject = SomeValueOrFunction
End If

The question you wanna ask yourself, though, is: do you want to start copying C# syntaxes/operators verbatim, and at what point does that become “not VB like, anymore”?

Case in point: Oxygene is arguable much closer to C# than VB is (even though it too is very distinct), and ?= would feel totally out of place, just copied to Oxygene, since we don’t use “?” for “nullable” logic anywhere ion Oxygene. If we were to add this feature to Oxygene, it would need to look different.

Isn’t the same true for VB, if not more so?

In VB, ? is also for nullable types.
To create a nullable Boolean, you define it as Dim b As Boolean?

The null operators are also all ?:
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/null-conditional-operators

Edit: Sometimes VB is more like C# than you would expect.

1 Like

Discussion on VBLang about this: https://github.com/dotnet/vblang/issues/522

ok, that’s fine. In the end I’ll take your word for whats VB-like and what isn’t, I just wanted to make sure you (all) did consider this aspect of the language design process.

Thanks, logged as bugs://84296

I agree with you that this is not really VB-ish; I would have designed it complete different.
But it is like this since VB2010; question mark fore everything that is nullable.
This was the only missing piece, so I took the same approach to make it fit in the already existing way of doing nullables.

How I should have done it if nullable wasn’t already implemented:
Dim i As Nullable(of Integer) instead of Dim i As Integer?
i = SafeNull(myString.Length) instead of i = myString?.Length
AssignIfNotNothing(i, 25) instead of i ?= 25

But we have to deal with backward compatibility where we have to follow the path of wrong design decisions that were made by Microsoft in the past.

And as everything that is nullable in VB is designed as ? by Microsoft, the best is to follow this bad design to keep things logical.

1 Like

Agreed then that this is the way to go, yes.

bugs://84296 got closed with status fixed.