Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0️⃣ Start enabling nullable #195

Merged
merged 7 commits into from
Oct 16, 2020
Merged

0️⃣ Start enabling nullable #195

merged 7 commits into from
Oct 16, 2020

Conversation

paulcscharf
Copy link
Member

@paulcscharf paulcscharf commented Oct 12, 2020

✨ What's this?

This enables nullable as errors in all projects and makes the necessary changes.

🔗 Relationships

Ref #184
Ref #194 (a pre-requisite for this PR)

🔍 Why do we want this?

Nullability is hip.

🏗 How is it done?

I enabled nullable and made sure they end up as errors in all project files. Then it was as simple as hunting errors and fixing case by case until everything compiled again.

💥 Breaking changes

This is a breaking change for anyone who has nullable enabled themselves, as some of our public members are now explicitly nullable.

🔬 Why not another way?

This is the most straight forward. The changes are pretty minor and splitting it into multiple PRs seemed silly, with the exception of #194, which includes several major changes.

💡 Review hints

I'll leave comments on all changes to explain why they are correct. Feel free to review based on that or however else you like.

Note that until #194 is merged into master and this PR brought up to date, the main project will not build. There is no conflict between the changes however.

@@ -2,9 +2,11 @@
<PropertyGroup>
<TargetFrameworks>net472;netcoreapp2.2</TargetFrameworks>
<LangVersion>8</LangVersion>
<Nullable>warnings</Nullable>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I could find, errors need to be enabled manually. Would like to know if there's a better way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick search pointed me to the fact you may be able to replace these 3 codes with the word "nullable"

See dotnet/roslyn#41605

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks! Turns out this causes a ton of other errors, so I will leave it at the above warnings for now and submit further PRs to achieve full nullability for easier review.

Bearded.Utilities.Tests/Core/MaybeTests.cs Show resolved Hide resolved
Bearded.Utilities/Collections/PrefixTrie.cs Show resolved Hide resolved
Bearded.Utilities/Core/Maybe.cs Show resolved Hide resolved
Bearded.Utilities/IO/Logger.cs Show resolved Hide resolved
Bearded.Utilities/IO/Logger.cs Show resolved Hide resolved
Bearded.Utilities/IO/Logger.cs Show resolved Hide resolved
@@ -234,7 +234,7 @@ public static T RandomElement<T>(this IEnumerable<T> source, Random random)
{
throw new InvalidOperationException("Sequence was empty.");
}
return current;
return current!;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above check on count ensures that this will never be null (unless in fact the sequence contained a null value, in which case that is the intended behaviour).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional:

Same comment as in the other PR, but it may not be a bad practice to always add a comment documenting cases where we use ! in a non-obvious way in code to avoid future confusion.

@@ -129,7 +129,7 @@ public T RunAndReturn<T>(Func<T> action)
{
var ret = default(T);
RunAndAwait(() => ret = action());
return ret;
return ret!;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of RunAndAwait ensures that the return value will be set. It may of course be set to null, if that is what the action returns, but that is entirely up to the caller.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem

Copy link
Member

@tomrijnbeek tomrijnbeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all my comments are optional, but it would be good to try if we can replace the warning numbers with the "nullable" string as suggested.

@@ -2,9 +2,11 @@
<PropertyGroup>
<TargetFrameworks>net472;netcoreapp2.2</TargetFrameworks>
<LangVersion>8</LangVersion>
<Nullable>warnings</Nullable>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick search pointed me to the fact you may be able to replace these 3 codes with the word "nullable"

See dotnet/roslyn#41605

Bearded.Utilities/Collections/PrefixTrie.cs Show resolved Hide resolved
Bearded.Utilities/IO/Logger.cs Show resolved Hide resolved
Bearded.Utilities/IO/Logger.cs Show resolved Hide resolved
@@ -234,7 +234,7 @@ public static T RandomElement<T>(this IEnumerable<T> source, Random random)
{
throw new InvalidOperationException("Sequence was empty.");
}
return current;
return current!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional:

Same comment as in the other PR, but it may not be a bad practice to always add a comment documenting cases where we use ! in a non-obvious way in code to avoid future confusion.

@@ -129,7 +129,7 @@ public T RunAndReturn<T>(Func<T> action)
{
var ret = default(T);
RunAndAwait(() => ret = action());
return ret;
return ret!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem

@paulcscharf
Copy link
Member Author

Explaining the null forgiving operators made me find another issue: #197 so that's great.

I'd like to merge it like it is now (after the bin packing PR), and then submit another PR with all nullable errors fixed (or perhaps multiple if there are other things that require big changes).

@paulcscharf paulcscharf changed the title Enable nullable 0️⃣ Start enabling nullable Oct 16, 2020
@paulcscharf paulcscharf merged commit 28f3aa5 into master Oct 16, 2020
@paulcscharf paulcscharf deleted the nullable branch October 16, 2020 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants