vkhorikov/CSharpFunctionalExtensions

[Question] What is the purpose of Result<T> for failures?

Closed this issue · 2 comments

A failure result cannot have a value, and attempting to access its Value will throw. So Result<T> only makes sense for success, not for failure.

So what is the point of the factory methods Result.Failure<T>(string error) and Result<T, E> Failure<T, E>(E error).

Am I misunderstanding? Surely those factory methods are just dead code?

Well I feel dumb for asking that, it's absolutely necessary.

e.g.:

public Result<User> findUser() {

  // ...happy path
  return Result.Success<User>();

  // ...error
  return Result.Failure<User>("error");     // <--- here is the reason, must match method signature
}

Yep, different branches of the class.