0

Hello i am trying to implement my own higher-order extensions but i can not understand why the aggregate (fold) gives the following error :

Error:

Cannot convert lambda expression to intended delegate
type because some of the< return types in the block are not implicitly convertible to the delegate return type.

I do not understand why it says the return types don't match.I use as a seed a List<object> and in the Fold for every element i add the element to the list and return the List<Object>.

Extension class:

public static class HOrder
{
    public static IEnumerable<U> Map<T,U>(this IEnumerable<T> collection, Func<T, U> transform)
    {
        var map = collection.Select(transform);
        return map;
    }
    public static TAccumulate Fold<TAccumulate,TSource>(this IEnumerable<TSource>source,TAccumulate seed,Func<TAccumulate,TSource,TAccumulate>aggregate)
    {
        var fold = Enumerable.Aggregate(source, seed, aggregate);
        return fold;

    }
    internal static object Extractor(Node node)
    {
        object result;
        switch (node.Kind)
        {
            case Node.Discriminator.String: result = node.AsStirng.Value; break;

            case Node.Discriminator.Integer: result = node.AsInteger.Value; break;

            case Node.Discriminator.Array: result = 
                 HOrder.Fold<Node,List<object>>(
                        node.AsArray.Items, 
                        new List<object>(), 
                        (x, y) => { y.Add(Extractor(x)); return y; } //<--Error at return y
                        );
             break;

            default:throw new NotImplementedException();
        }
        return result;
    }
}

P.S : The error is highlighted on the return y.

9
  • But i am not saying something like if T ==Y then List<T>== List<Y> .The type is List<object> and i am not doing any type inference.It is crystal clear the signature. I just use a side-effect to add an item to an already existing list. Commented Jun 18, 2018 at 8:28
  • It's hard to help with an incomplete example. We need the Node type in order to reproduce this for ourselves. Could you either include Node or reduce the problem to something smaller using system types?
    – Jon Skeet
    Commented Jun 18, 2018 at 8:36
  • 1
    Change (x, y) => to (y, x) => and it should work. One reason why single letter variable names suck sometimes. Though... I'm surprised you weren't getting errors for the Add/Extractor calls. Actually... You have them reversed in your call to Fold<> as well. It should be Fold<List<object>, Node> since accumulate comes first and source second Commented Jun 18, 2018 at 8:47
  • 1
    Other than using your preferred names, what are the supposed advantages of just wrapping Select and Aggregate LINQ functions? Commented Jun 18, 2018 at 8:51
  • 1
    If you rely on type inference the second issue goes away (the reverse type arguments in the call to Fold) then it's a matter of fixing your lambda to match the Func signature. HOrder.Fold(node.AsArray.Items, new List<object>(), (x, y) => { x.Add(Extractor(y)); return x; }); though I see no benefit of using this over Aggregate directly. What's more, since you've defined these as extensions, you can call them as such: node.AsArray.Items.Fold(new List<object>(), (x, y) => { x.Add(Extractor(y)); return x; }); Commented Jun 18, 2018 at 8:55

2 Answers 2

0

I have solved the problem by switching the generic parameters from Fold<Node,List<object>> to Fold<List<object>>,Node>.

-1

Btw, @pinkfloydx33's comment on the original question was my solution! I dislike single letter lambda variables!!

Blockquote Change (x, y) => to (y, x) => and it should work. One reason why single letter variable names suck sometimes. Though... I'm surprised you weren't getting errors for the Add/Extractor calls. Actually... You have them reversed in your call to Fold<> as well. It should be Fold<List, Node> since accumulate comes first and source second – pinkfloydx33 Jun 18 '18 at 8:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.