Skip to content

C# 8 nullable support #691

Closed
Closed

Description

@jl0pd

What can we do to make ScottPlot better?

Consider adding nullable annotations as they will increase productivity of using library.
This will clarify what you may expect from function.

For example:

public static int[] RandomInts(Random rand, int pointCount, double multiplier = 1, double offset = 0)

Only those who read source code would know that they may pass null as first parameter

Or:

public double[] MinorFromMajor(double[] majorTicks, double minorTicksPerMajorTick, double lowerLimit, double upperLimit)

This may not be obvious that if you pass array with less than 2 elements it will return null

Is this breaking change?

No. Nullable annotation are not breaking existing code. Types remain same, but with some attributes, not changing behavior. It will work with .Net Framework, I've tested it with .Net Framework 4.0 and 4.6 in different projects

Activity

  1. bclehmann commented on Jan 11, 2021

    @bclehmann
    Member

    What exactly do you mean my nullable annotations? For C# I only see the two options:

    <Nullable>enable</Nullable>

    Or:

    #nullable enable
    #nullable disable
    .
    .
    .
    etc

    Source

    The above set both the nullable warning and annotation context, but there are preprocessor directives to set only one or the other. As I understand it, the annotation context changes some of the rules of C# at compile-time, it does not indicate to the user that some parameters may be null or that some return values may be null

    I admit that the behaviour of those two methods is pretty unintuitive and I think addressing that is a worthy goal.

    If I am confused about what you mean by nullable annotations then please let me know.

  2. StendProg commented on Jan 11, 2021

    @StendProg
    Contributor

    Hi there,
    as I understand:

    #nullable enable

    prevents null assignment to reference types (compile time warnings/errors).

    If you want to use null, then you must specify it explicitly:

     public static int[] RandomInts(Random? rand, int pointCount, double multiplier = 1, double offset = 0)

    This is all the correct approach, of course, but everyone so used to nullable reference types by default, that I think it, on the contrary, will worsen readability...

  3. jl0pd commented on Jan 11, 2021

    @jl0pd
    Author

    What exactly do you mean my nullable annotations?

    By that I mean annotations inside of code with ?

    For example if I define method like this

    int[]? MyMethod(string label, int[]? X);

    And try to call it like this MyMethod(null, null) I'll get a warning under first null, because method told compiler that it waits for non-null value, but second null will pass without warning. Also it will inform that return value is also maybe null and I must to check it before use

    MyMethod(null, new int[0]).Length; // warning. Possible null reference
    
    int[]? values = MyMethod(null, new int[0]);
    int le;
    if (values != null)
        le = values.Length; // no warning
    else
        le = values.Length; // warning

    There's great talk at YT "Jared Parsons — Nullability in C#", it explains why, when and how

  4. bclehmann commented on Jan 11, 2021

    @bclehmann
    Member

    My apologies, I did not see that you wish to change the types of some reference types to their nullable counterpart.

    I will admit, I have not used nullable reference types in C# before, they're a pretty new feature, plus they haven't seen much adoption as most projects don't want to switch over all at once, nor do they want to have half their project using C# with different compilation rules.

    That said, I think this is an improvement, so long as it does not require the user to make code changes, for example, nullable value types can require this:

    if(x.HasValue){
        foo(x.Value);
    }

    It's my understanding that nullable reference types use static code analysis to determine if a non-nullable reference type could be null each time it's dereferenced, and only in regions which have the required annotations/warnings enabled. Since it doesn't affect the user (as far as I know) I don't object to it. However, having not used nullable reference types (in C#) I wouldn't be able to comment on how useful or how nice to write code with them is, so someone may well object on those grounds.

  5. swharden commented on Jan 19, 2021

    @swharden
    Member

    Hi @jl0pd, @Benny121221, and @StendProg, this is a very interesting discussion!

    Primary Issue

    Much of the discussion above relates to nullable reference types, but there are features like switch expressions, indices and ranges, and records that may also be nice to have. Putting aside the topics of features and syntax, these are the questions I am unsure of:

    • Should ScottPlot's C# language version be increased?
    • If so, which version should be used? 8.0, 9.0, latest, or latestMajor?

    Current Target and Version

    ScottPlot targets .NET Standard 2.0 (which has a default language version of 7.3).

    <TargetFramework>netstandard2.0</TargetFramework>

    <LangVersion>7.3</LangVersion>

    Upgrading that target would mean dropping support for some existing platforms, so let's take that option off the table for this discussion. The ScottPlot library is used by controls which target back to .NET Framework 4.6.1, so moving forward I want to make sure that functionality is preserved.

    Does increasing version have any negative consequences?

    C# 7.3 is the default language for .NET Standard 2.0. Can we increase it to whatever we want, like latest or 9.0? As @jl0pd pointed out, I tried it on my system it seems to compile fine... I didn't expect it would be that easy, and I'm still not too sure what to make of it. Is it really that easy? Can we target any version we want in any package we want?

    According to Microsoft: C# language versioning:

    • C# 8.0 is supported only on .NET Core 3.x and newer versions
    • C# 9.0 is supported only on .NET 5 and newer versions
    • Choosing a language version newer than the default can cause hard to diagnose runtime errors ☠️

    As seen by PR #692 switching to newer language features potentially touches a lot of files, so making all those changes then walking them back if we hit run-time errors would be a pretty lousy way to move forward. I'll keep reading on this topic, but for now I'm pretty apprehensive about upgrading the language version beyond the default.

    I certainly welcome input if anyone has more experience in this area! Thanks again @jl0pd for the issue and PR. Regardless of how the language version decision plays out, your PR pointed-out a lot of methods which could benefit from more clear documentation and handling of potentially null arguments.

  6. swharden commented on Jan 23, 2021

    @swharden
    Member

    I haven't seen active discussion in the last few days so I'll probably let this sit here for a while longer and see if any new conclusions come out.

    I'd like to convert to this to a Discussion, but it looks like GitHub is temporarily disabled conversion of issues to Discussions (community/community#2924), so I'll add a tag and convert it later.

    @jl0pd, thanks again for your contribution here and very interesting discussion! I'm excited to pick this up again when the time is right 👍

  7. jl0pd commented on Jan 23, 2021

    @jl0pd
    Author

    Was bit busy during week, but now I can return to discussion.

    About language version, I think that the reason why MS told that "C# 8 supported only on netcore3.1" is because they don't wanna meet reports like "I've updated to C# 8.0 and tried to use slices, but compiler says that System.Range is not defined".
    There's no one-to-one relation between runtime and compiler version. Compiler is aware of runtime, when runtime isn't aware of language. Some features of newer language versions require new types, like System.ValueTuple for C# 7 or System.Range for slices in C# 8. But some isn't, like switch-expression or ??= - it's just sugar on top of existing features. Switch-expression compiles into bunch of if-else or switch depending on usage. Language version doesn't affect assembly when somebody referencing it. So I think that version should be latest.

    Using nullable reference types requires several attributes inside System.Diagnostics.CodeAnalysis namespace, but nobody tells that they should be inside mscorlib/System.Private.CoreLib. Current behavior is that compiler emits System.Runtime.CompilerServices.NullableAttribute in any assembly built with nullable annotations. This behavior allows us to implement all kinds of attributes in our own assembly and everything will work until somebody tries to access these attributes via reflection. It won't crush, but behavior maybe incorrect. Defining attributes on our own is dangerous, because then we may stumble into compiler errors when target netcore3.1/net5.0. Nullable package (which is included in src/ScottPlot/ScottPlot.csproj) relies on this behavior and provides safe way of enabling NRT inside any version of dotnet.

    We can start with enabling multitargetting. This enables our project to target different runtimes and take best of new versions, saving backward compatibility. All that needs to be done is change this attribute <TargetFramework>netstandard2.0</TargetFramework> to this <TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>. Compiler knows about multitargetting and gives hints about incompatible features, so this would be safe.
    image
    image

  8. swharden commented on Jan 23, 2021

    @swharden
    Member

    @jl0pd, no worries on the delay, and thank you for your very informative reply! This really helps my understanding of the issue, and a plan is emerging but a few questions remain. I have limited experience when it comes to multi-targeting on .NET and some of these questions may seem basic, but I'm very interested to get your input on this plan.

    ✔️ Enable multi-targeting in ScottPlot.csproj

    Right now the user controls use multi-targeting, but the core ScottPlot library does not. It just targets netstandard2.0. It sounds like I can immediately add more targets to the core library

    • netstandard2.0 (default C# 7.3)
    • net5.0 (default C# 9.0)

    Are there any advantages to adding more targets? For example,

    • netstandard2.1 (default C# 8)
    • netcoreapp3.1 (default C# 8)
    • net48 (default C# 7.3)

    ✔️ Don't lock C# version at 7.3 (use latest)

    The default value of LangVersion is latestMajor, and I can change it to latest.

    As shown in your screenshot, it looks like the compiler knows which runtime features are supported by .NET Standard 2.0, and will warn if users add them in (even though the language version technically supports them).

    ❓ Add a dependency to a third-party library to enable Nullability

    Nullable package relies on this behavior and provides safe way of enabling NRT inside any version of dotnet

    I really like this package and what it does. Fundamentally, I really like the concept of tightly defining nullability, and am totally on board with the advantages it can bring to a code base.

    However, I notice this package with 83 stars was created about a year and a half ago by a GitHub user. I'm extremely apprehensive about coupling ScottPlot to this dependency, and I'm highly motivated to keep ScottPlot dependency-free (with the exception of official NuGet packages maintained by Microsoft). That said, I see a lot of packages depend on it (including at least one managed by Microsoft)... but overall I'm extremely apprehensive about this and thought I'd share my concern.

    Even if this library is not used, the changes listed above can still be made and can have a positive impact on this project.

    I'm looking forward to hearing your thoughts!
    Scott

  9. jl0pd commented on Jan 24, 2021

    @jl0pd
    Author

    Are there any advantages to adding more targets?

    It is recommended to also target net461 when you target netstandard2.0. Newer versions dotnet framework seems to work fine with netstandard2.0 directly, see docs for more info. I think it's best to use

    • net461 as mentioned in docs
    • netstandard2.0 for net472+ & netcore2.0+
    • net5.0 for newer versions.

    Adding more targets seems unnecessary. As the result tag should be <TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>.

    ❓ Add a dependency to a third-party library to enable Nullability

    This library provides an easy way of adding nullable attributes. We can use this or we can define attributes on our own. We know exactly used target frameworks, so we can manage enabling and disabling manually.
    All of the attributes should be defined as internal to avoid compile-time errors in customer codebase. It should look like that:

    namespace System.Diagnostics.CodeAnalysis
    {
    #if !NET5_0
        internal class AllowNullAttribute ...
    #endif
    }

    Source code can be taken from dotnet/runtime. Code is licensed under MIT which allows us to copy & modify but I'm not entirely sure about

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software

    Do we have to copy license, or we can copy, modify code and it will become ours, and we don't have to use original license.
    For example in Nullable repository has no mention of dotnet foundation even when attributes are taken from dotnet/runtime.

    Would be nice if anyone can explain what to do, because I don't understand how it works.

  10. swharden commented on Jan 24, 2021

    @swharden
    Member

    Thanks for your feedback @jl0pd!

    Multi-Targeting

    The Cross-platform targeting docs are excellent, and although I've seen that page before I previously overlooked:

    CONSIDER adding a target for net461 when you're offering a netstandard2.0 target

    I'll make this change and publish a NuGet package later today.

    Defining Nullable Attributes

    I like your suggestion of implementing our own attributes, which cuts the dependency on the external library. Technically using #if !NET5_0 would probably break when .NET 6 comes out, but I get the gist of the suggestion and will explore it further soon.

    There are several issues, PRs, and warnings related to ScottPlot 4.0 → 4.1 API changes that I need to resolve, and I may prioritize these over supporting nullability in the short term. I'll leave this issue open until the time is right to make this my primary focus for a while.

    License

    I feel pretty good about this one since the .NET Runtime, the Nullable package, and ScottPlot are all MIT licensed. I think including a comment indicating original copyright at the top of NullableAttributes.cs (which is conveniently already there) would satisfy the requirements.

  11. swharden commented on Jan 24, 2021

    @swharden
    Member

    Following-up, multi-targeting revealed some compile errors which were very helpful in better designing this project! I'm very happy these issues were discovered.

    <TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>

    .NET Framework 4.6.1 does not support C# 7.0 language features (value tuples), but this fixes it:

    <!-- .NET Framework 4.6.1 requires packages to support C# 7 features -->
    <ItemGroup Condition="'$(TargetFramework)' == 'net461'">
    <PackageReference Include="System.ValueTuple" Version="4.5.0" />
    <PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
    </ItemGroup>

    I set C# language version to latest by default, but restricted it to 7.3 for .NET Framework builds EDIT: see details below

  12. jl0pd commented on Jan 25, 2021

    @jl0pd
    Author

    I set C# language version to latest by default, but restricted it to 7.3 for .NET Framework builds

    Why? dotnet framework builds fine with newer versions of C#. It will only bring problems.

    public int Foo(int x) => x switch { 1 => 2, _ => x }

    This could compile in any target, but contributors will have to use net461 when editing to avoid compilation errors.

    image
    image

    Worst part is that it compiles only with targets other than net461. Somebody may build under net5 and see that everything is ok, but once commit is merged and you try to publish it to nuget you may stumble in compilation errors.

    big console screenshot

    image

    In this case for project to compile people will have to give up on new features, or use conditional compilation, which will bring much of duplication and reduce readability

    Use one LangVersion of entire project

  13. swharden commented on Jan 25, 2021

    @swharden
    Member

    Thanks for your patience on this one @jl0pd! I'm not sure why this has been so difficult for me to understand.

    My hang-up was that I thought defining a newer C# version would permit users to insert code which would build without error but crash at runtime depending on which features were used. This concern was amplified by Microsoft docs saying, "Choosing a language version newer than the default can cause hard to diagnose runtime errors". My intent for defining language version 7.3 for .NET Framework 4.6.1 was to try to police people's use of those features and move those perceived runtime errors to compile time errors (which the CI pipeline would rapidly detect as a failed build). I now see that my understanding was flawed, and this is not necessary.

    Like you suggested in #691 (comment), I now understand that any C# project can be moved to any newer language version at any time, and new language features which can be compiled-down to something runnable on the old target framework (like switch expressions) will be, but features which can't (like records) will throw build errors (not runtime errors). This last part finally makes sense to me now, and makes it clear that I can use the latest C# version everywhere, and if I try to implement features truly not supported by older targets I'll be warned of this at compile time.

    Thanks again for the feedback!

    Resources Mentioned Above

  14. locked and limited conversation to collaborators on Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions