-
Notifications
You must be signed in to change notification settings - Fork 210
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
Avoiding proliferations of ifdefs for nullable types #4255
Comments
Hi @Petermarcu, |
The name you use has to be one recognized by the C# compiler. @KrzysztofCwalina is the person I would reach out to to make sure you get it right. |
Thanks for the additional information @KrzysztofCwalina could we imagine a situation where we still place those abstractions library with the expected name, but we fully qualify them in the generated code to disambiguate from the default framework ones? |
If your users use the compiler nullability syntax, I think you can get away with multiple copies of the same named attributes in scope (@jaredpar would know for sure), which can happen if you ship them as public types in a reusable package. But we don't do things like that in .NET libraries because sooner or later users will refer to these attributes directly (some don't have syntax) and that can cause ambiguities or at least confusions when trying to debug. |
right, but since the code we're trying to simplify here is generated, we need to place that annotation somewhere. |
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
public string? ActiveLockReason { get; set; }
#nullable restore
#else
public string ActiveLockReason { get; set; }
#endif I don't understand the need for this pattern though. Generally the way that most customers approach this is to just do the following: public string? ActiveLockReason { get; set; } Then just include the attributes which aren't autogenerated by the compiler inside
In what way are they not compatible? We use them in the C# compiler just fine with targeting |
Allow me to provide additional context here: people can use kiota to generate code which ends up in a netstandard2.0 project. We need to ensure the projected code will compile properly within that context. (this is also why we don't rely on file scoped namespaces) As far as I know, netstandard2.0 does not support nullable reference types were introduced with CSharp 8 and the annotations are only available with netstandard2.1 and netcore3.1 or higher By default:
Unless the source either has:
The warning can turn into an error if the csproj has Treat Warning As Error (many people do so to "block" things in order to avoid building technical debt).
the following <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TreatWarningAsError>true</TreatWarningAsError>
</PropertyGroup>
</Project> namespace dotnetlib
{
public class Class1
{
public string? Id {get;set;}
}
} will result in If we can get rid off the if def while maintaining compatibility with a netstandard2.0 project, I'm happy to implement the solutions. |
Okay I think I understand the issue a bit better here. Essentially in
Have you considered letting users specify the language version or target framework they are using on the command line? Essentially something like |
Yes, this is something we're fundamentally against as we want to keep the CLI and the maintenance matrix simple. As an example, typescript has could have about a dozen switches for the level of compatibility of the language, the module types, etc... We want to prevent that sprawl of switches that will degrade the experience. |
Thanks everyone for the input here. Closing this one since, given our requirements, the dotnet team members on this thread has reached the same conclusion. |
example here: https://github.com/octokit/dotnet-sdk/blob/main/src/GitHub/Models/Issue.cs
You could consider doing something similar to the Azure SDK where local attributes that the compiler respects are defined and work down to netstandard2.0. This prevents having to emit code with all the conditionals everywhere.
Note, that these local attributes should only ever be internal in shipping libraries or they will cause conflicts with other libraries.
https://github.com/Azure/azure-sdk-for-net/blob/69c969a752cf07ae4ae7c67abdc2ddc8da209693/sdk/core/Azure.Core/src/Shared/NullableAttributes.cs
The text was updated successfully, but these errors were encountered: