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

Nullable reference types support #30

Open
adambajguz opened this issue Oct 4, 2022 · 2 comments
Open

Nullable reference types support #30

adambajguz opened this issue Oct 4, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@adambajguz
Copy link

.NET slowly transisions to code that uses nullable reference types. It would be great if Validot supports nullable reference types.

Problem:

public record Model
{
	public string? Language { get; init; }
}

public sealed class ModelValidator : ISpecificationHolder<Model>
{
	public Specification<Model> Specification { get; }

	public ModelValidator()
	{
		Specification = m => m
			.Member(x => x.Language!, language => language // must use a null forgiving operator to use the IsLanguageCode extension 
				.IsLanguageCode()
			);
	}
}

public static IRuleOut<string> IsLanguageCode(this IRuleIn<string> @this) // string doesn't allow a null
{
	// some code
}

I can replace IRuleIn<string> with IRuleIn<string?> but "If the value entering the scope is null, scope commands are not executed." (DOCUMENTATION.md#null-policy) and using null forgiving operator is for me awful.

Nullable support in .NET Standard 2.0

Nullable package can be used to add nullable reference types to .NET Standard and/or a multi-target package can be created (I've never checked how e.g. .NET 6 handles nullable annotations from .NET Standard 2.0 package because I've always used multi-targeted packages so this need verification).

 <PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <Nullable>annotations</Nullable>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'">
    <PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="all" />
  </ItemGroup>
@adambajguz adambajguz added the enhancement New feature or request label Oct 4, 2022
@bartoszlenar
Copy link
Owner

Hi @adambajguz , thank you for pointing this out.

Yes, I've been thinking about it intensively for a year now. Recently, after releasing 2.3, I made a decision and because thanks to you there is an issue about nullable reference types, I feel obliged to share my plans for Validot in that matter.

Two statements to start with:

  • Nullable reference types are the future and Validot handles them poorly. I don't like reading code screaming at me.
  • Validot depends on the framework and on the framework only. That's one of the principles I've taken and I'll continue to follow it. Also, I'm not an expert in multi-target packages, nor am I willing to become one.

What's the solution, then?

  • At the end of 2022 Microsoft ends the support for the last .NET Core version (3.1). From this moment, all LTS versions will be the "new" .NET.
  • Validot policy will be to target the framework version that is (at the moment of the release) the LTS with the longest time on the market (so, the oldest still supported LTS).

What's the plan?

I'll release two or three versions with as many features (for sure #5 and #26) as I can code within reasonable amount of time. So let's say there will be 2.5, 2.6 and 2.7 still targeting .NET Standard 2.0. This way I'll ensure that features achievable now are available for the broader audience with legacy projects.

Only then I'll redirect my efforts to making version 3.0, which will include native support for nullable reference types on top of other minor breaking changes.

@adambajguz
Copy link
Author

Sounds like a reasonable plan! Thanks for sharing it!

@bartoszlenar bartoszlenar added this to the v4.0 milestone Dec 14, 2022
@bartoszlenar bartoszlenar modified the milestones: v4.0, v3.0 Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants