Search code examples
language-ext

language-ext Task of Either with multiple from clauses


I am learning FP with language-ext and I ran into a problem that I have not been able to overcome. I simplified my code down to this example:

using System;
using System.Threading.Tasks;
using LanguageExt;
using static LanguageExt.Prelude;
using Xunit;

namespace Temp {

    public class SelectManyError {

        [Fact]
        public async Task Do() {

            var six = await from a in Task.FromResult(Right<Exception, int>(1))
                            from b in Task.FromResult(Right<Exception, int>(2))
                            from c in Task.FromResult(Right<Exception, int>(3))
                            select a + b + c;

        }
    }
}

I am getting this error:

Multiple implementations of the query pattern were found for source type Task<Either<Exception, int>>. Ambiguous call to 'SelectMany'.

I understand what the compiler thinks the issue is from reading this webpage. But, I am clearly missing or not understanding something important because I cannot figure out how this error is caused by this scenario OR what to do about it. This will work just fine if it is only 2 from clauses, which confuses me even more.

Is this the wrong approach to this type of problem? Is there another way I am unaware of?


Solution

  • The compiler is having a hard time understanding what the type of a is supposed to be (either int or Either<Exception, int>) since it is unused on the second from line.

    Here is a awfully ugly workaround for this specific case. However, for any type, I think the hack can be adapted to work for that type.

    using System;
    using System.Threading.Tasks;
    using LanguageExt;
    using Xunit;
    using static LanguageExt.Prelude;
    
    public class Namespace
    {
        [Fact]
        public async Task Method()
        {
            var six = await from a in Right<Exception, int>(1).AsTask()
                            from b in Right<Exception, int>(a - a + 2).AsTask()
                            from c in Right<Exception, int>(3).AsTask()
                            select a + b + c;
        }
    }