Nullable reference types is a new feature introduced in C# 8. In short, when it is enabled, any type that is not explicitly marked nullable cannot be null or it will produce a warning or error (depending on how strict it is configured). This is already the case with structs, enums and primitive types and would just extend to normal classes too.
Here is some sample code:
string a = null; // Error or warning depending on how strict it is set
string? b = null; // Works fine, string is now declared nullable
public void DoSomething(string? someParameter)
{
someParameter.Something(); // Error or warning: someParameter can be null here
}
The compiler can also analyze when a variable cannot be null:
if (someParameter != null)
{
someParameter.Something(); // no error or warning
}
Proposal
I think the S&box API should at least be nullable annotated so that addons which want to enable nullable reference types can utilize it fully. It also makes clear what methods expect and return, e.g. if the return value and parameters can be null or not.
Why
Remember all the nil
spams in Garry’s Mod? We surely don’t want to have the same in S&box with NullReferenceExceptions. This is not just an issue related to Garry’s Mod, it is a general issue in programming languages that have a concept of null
. Tony Hoare, the inventor of null
, called it his billion dollar mistake in 2009 for that very reason.
I believe having this enabled by default for new addons (either as warning or even better as error) would improve the overall quality of addons. It does not make writing addons any harder either. Anyone who has used nullable structs or nullable ints already knows how it works. In any case, addons should be able to either disable or enable this depending on their own needs.
Examples
Here is an example issue from OpenMod that was reported before OpenMod enabled nullable reference types:
After I have enabled nullable reference types and started migrating existing code to it, the compiler started erroring out related code:
With nullable reference types I was able to fix known bugs like this immediately.
Enabling nullable reference types also helped discovering many yet unknown bugs, like this one:
User data was never loading from disk as m_CachedUsersData never could be null.
Here is the related commit for reference. It took about 2 days to convert this project which consists of about 40k lines of code.