0

EDIT: My gist seems to work, ie, I dont get any problems... so I guess the problem lies elsewhere. Ill keep this post open for a short while, then close it

I have this class Result

public class Result<T>
{
      public T? Data { get; private set; }      
      public Result(T? data) => Data = data;          
      public Result<T> Validate(Func<T, bool> validator, Func<T, string> errorMessage)
      {
          if (this.Data != null)
          {
               return validator(this.Data!)
                  ? this
                  : Result.Error<T>(errorMessage(this.Data!));
          }
          return this;
      }
}

public static class Result
{
    public static Result<T> From<T>(T? data) => new Result<T>(data);
    public static Result<T> Error<T>(string message) => throw null!;
}

I want to write

Result result = Result.From("hello");
               .Validate(t => t != "world", t => "You cant write world")

This works, but I get that squiggly line on the variable t in the Validate-call, saying t might be null, even though I make a null-check (and bang operator).

Is there any way to convince the static null checker that t cant be null here? (null check is done in From-function, but that is irrelevant here.)

Cowborg
  • 2,645
  • 3
  • 34
  • 51
  • 1
    [Failed Repro](https://sharplab.io/#v2:C4LgTgrgdgPgAgJgIwFgBQ6DEUIBtcCGARrgKYAEpUxZ66ASqQM57AA8cSADAHzljNW5ALzlGLXMAB0AMTAB7ALYAKAEQALUvnmqAlOnKGjxk1IBqBXAEsAJgWCllwEX2cBCUaoDu8sLhuqADTkzsJ8qgCa8hDkAMYEUM5eYFYO5D5+AboA3HRocADM5IhigpJsACo86ADeBiaF5BXkACL2BOQ15ADmpMDZ5AAOKQBu9hRMfQMAvvXGc0aN4qzKzXbABLoure0i5OsEuWgmJguGS2XsVeQW1uuOcACslcFE8vK4fGN39r7BTy9itw+KQwAowABZZhMAi9fTHEx1BEnQxWABm5Cc6isTCkbQ25A85Bw+HhKKMSPJKLgAHZyN9bL8wFicXj2m4yVTyQB+ELYphnLlGEClCTSACiYN8lR4ylB4KhTBhvRZuPxBA5OUFxlmyOpdOA/KOJ11uvQjU4ADZighRaxagsLUhrctytc5EoZat9u0tmFiaQvHa3bKDlq9U6XZcZeRJeCvZwuORFNDYaQ/a51AogyTcG4jtMgA=) – canton7 May 03 '22 at 09:39
  • 1
    Cannot reproduce. Please make sure your question contains a [mcve] -- I had to fix multiple issues to get the code in your question to compile, and it doesn't reproduce your error. – canton7 May 03 '22 at 09:39
  • Try with `null-forgiving` [operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving) – McNets May 03 '22 at 09:50
  • Sorry guys, I should have created a gist, its created now (updated the post) . The big difference is data is nullable.. – Cowborg May 03 '22 at 10:02
  • @McNets: the code already has a null-forgiving operator – Cowborg May 03 '22 at 10:06
  • **EDIT**: [My gist][1] seems to work, ie, I dont get any problems... so I guess the problem lies elsewhere. Ill keep this post open for a short while, then close it – Cowborg May 03 '22 at 10:17
  • I'm sorry but I can't see the null-forgiving operator in your code. – McNets May 03 '22 at 10:19
  • @McNets its here for example "return validator(this.Data!)" – Cowborg May 03 '22 at 10:37
  • Note that the `!` in `return validator(this.Data!)` isn't needed -- take it out, and you still don't have a warning. The compiler knows that `this.Data` isn't null from the `if (this.Data != null)` check above – canton7 May 03 '22 at 10:39
  • yes, that is true, I was just grasping for straws there for a while, trying to convince static analysis – Cowborg May 03 '22 at 10:56

1 Answers1

1

...ok ... it was my fault (of course)

On creation I took T data, which if you send in a nullable object, returned a nullable object. But if I instead declared to take a nullable object and returned a non-object, it helped analysis to be sure what to pass through. Thanks for the help, It wasnt really helpable with the information I provided, since it was in the creation (From()), sorry about that

I changed from this

 public static Result<T> From<T>(T data, string? errorMessage = null) =>
        data is null
            ? Error<T>(errorMessage ?? $"Parameter value is null in Result.From(), expecting a non-null {typeof(T).Name}")
            : Ok(data);

to this:

 public static Result<T> From<T>(T? data, string? errorMessage = null) =>
        data is null
            ? Error<T>(errorMessage ?? $"Parameter value is null in Result.From(), expecting a non-null {typeof(T).Name}")
            : Ok(data);
Cowborg
  • 2,645
  • 3
  • 34
  • 51