CSharpFunctionalExtensions icon indicating copy to clipboard operation
CSharpFunctionalExtensions copied to clipboard

Another simple utility extension: Fail

Open kitsu opened this issue 6 years ago • 1 comments

Before I fully adopted this library I had my own shoddy imitation. I'm slowly upgrading, but I'm missing one method from my Result implementation: Fail.

In most cases it is trivial to just Result.Fail(message), but when dealing with complex type constraints it can be a pain. For example I am fixing up a method right now that has a Result<Dictionary<string, List<ContactInfo>>>, and to fail it I have to use Result.Fail<Dictionary<string, List<ContactInfo>>>(message). While that is very descriptive, I literally have the same type declaration two lines above where I'm failing. Also I don't really care about the type of a failure - the compiler does.

Here is the simple extension I've added for this (which doesn't cover all Result cases):

        public static Result<T> Fail<T>(this Result<T> result, string error)
        {
            return Result.Fail<T>(error);
        }

Use is just someResult.Fail(message), and the compiler's happy.

Just wanted to share in case anyone else has this problem.

kitsu avatar Mar 14 '19 22:03 kitsu

Nice workaround, I struggle with this issue myself too. Feel free to submit a PR if you are so inclined.

vkhorikov avatar Mar 18 '19 14:03 vkhorikov