Home > Back-end >  Get Right value from LanguageExt c#
Get Right value from LanguageExt c#

Time:10-27

I have method that returns: Either<Error, (int seconds, int count)> result = GetResult();

I need to return right value: (int NextDelayMs, int NextRetryCount)

But I can't figure out how to do it. I tried:

var toReturn = result.Map(x => x.Right);

but I get IEnumerable this way. What am I doing wrong?

CodePudding user response:

You have to handle the left case as an Either value can be left type L or right type R.

If you want to throw an exception when result is an Error you can use this:

var toReturn = result.IfLeft(err => throw err.ToException());

But you should avoid this. It's better to stay within Either using Map/Select and pass any errors to the calling method:

var errorOrFunctionResult = result.Map(x => someFunction(x));

If you want to supply a fallback value in case of an error:

var toReturn = result.IfLeft((100, 0))

Basically Either is similar to Option regarding usage and you can find a good introduction here: https://github.com/louthy/language-ext/#option

CodePudding user response:

    (int, int) yourFuncticon()
    {
        return (1, 2);
    }
  • Related