Creating a custom Either type with a custom fixed Left type. #1273
-
Hi everyone. Is there a way of creating an Either<L,R> where L is my custom error type? I can't inherit from Either<L, R> since it is sealed. My team is not willing to use the Error class, and hence Fin is of no use. I tried to inherit from Error and create custom errors, but unfortunately, Error is a record, and we are using CSharp 7.3. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
If you're not willing to update language version (which is a very weird position to take) then the only way is to wrap the existing public struct Thing<A>
{
readonly Either<YourErrorType, A> either;
public Thing<B> Map<B>(Func<A, B> f) =>
new Thing<B>(either.Map(f));
public Thing<B> Bind<B>(Func<A, Thing<B>> f) =>
new Thing<B>(either.Bind(x => f(x).either));
...
} |
Beta Was this translation helpful? Give feedback.
-
Thank you so much @louthy. It worked for me! |
Beta Was this translation helpful? Give feedback.
If you're not willing to update language version (which is a very weird position to take) then the only way is to wrap the existing
Either
implementation in a new type: