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

Add nullable reference type annotations #65

Closed
vers-one opened this issue Oct 6, 2022 · 0 comments · Fixed by #71
Closed

Add nullable reference type annotations #65

vers-one opened this issue Oct 6, 2022 · 0 comments · Fixed by #71
Assignees

Comments

@vers-one
Copy link
Owner

vers-one commented Oct 6, 2022

Description

EpubReader currently lacks nullable reference type annotations, mostly because it needs to target .NET Framework which locks the C# compiler version to C# 7.3 while nullable reference types require at least C# 8.0. However, there is a way to specify an explicit C# compiler version in csproj file via <LangVersion>x.x</LangVersion> project property. This should work even for .NET Framework and .NET Standard 1.0, as long as the code doesn't use any runtime features of the newer C# compiler. The only downside of this approach is the lack of nullable annotation attributes which require the project using them to NOT have any targets other than .NET Core >= 3, .NET >= 5, or .NET Standard 2.1. This leads to two main consequences:

  1. EpubReader cannot have any utility methods for nullable reference type annotations. For example, instead of creating a custom assertion method:
    void Assert([DoesNotReturnIf(false)] bool condition, string? message = null)
    {
        if (!condition)
        {
            throw ...
        }
    }
    it will have to copy-paste this check everywhere such assertion is required.
  2. .NET libraries themselves don't have any annotations so even after making a String.IsNullOrEmpty(test) check C# compiler still treats test as potentially null. The only workaround is to add an explicit if (test != null) { ... } check.

However, these downsides seem like reasonable tradeoffs for having nullable reference type annotations in EpubReader and most importantly, they don't affect the consumers of the library in any negative way.

Proposed solution

Switch to C# 10.0 compiler and add nullable reference type annotations for VersOne.Epub assembly.

Additional context

Documentation: https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant