user3779002
user3779002

Reputation: 620

C# generic error, complains it can't convert between identical tuple types

trying to do my own zip, got this code (generic type param T is defined on the enclosing class of this method).

public List<(T, T2)> Zip<T, T2>(SQLList<T2> that)
            where T2 : LDBSQLemitter {
    var res = this.Zip(that).ToList();
    return res;
}

error is

Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<(T First, T2 Second)>' to 'System.Collections.Generic.List<(T, T2)>'

What's this First/Second stuff in 'System.Collections.Generic.List<(T First, T2 Second)>', why is it missing in 'System.Collections.Generic.List<(T, T2)>' - - what is it about tuple types I don't understand here?

OK, use try instead to make an explicit conversion of each tuple, so

public List<(T, T2)> Zip<T, T2>(SQLList<T2> that)
                where T2 : LDBSQLemitter {
        var tmp = this.Zip(that).ToList();
    
        var res = new List<(T, T2)>();
        foreach (var x in tmp) {
            res.Add((x.First, x.Second));
        }
    
        return res;
    }

Now the error's completely baffling, apparently complaining it can't convert between 2 identical tuple types:

Error CS1503 Argument 1: cannot convert from '(T, T2)' to '(T, T2)'

Wassup?

Upvotes: 3

Views: 515

Answers (1)

Cameron MacFarland
Cameron MacFarland

Reputation: 71856

The problem is this, which the compiler can't relate to T.

It would help to know the signature of the class too. Does it also contain a generic parameter T? By also having a T generic parameter in the method you've defined T twice, and they are not the same.

You can fix it by explicitly casting this

var res = ((IEnumerable<T>)this).Zip(that).ToList();

or add Cast

var res = this.Cast<T>().Zip(that).ToList();

Or remove the extra T in the method

public List<(T, T2)> Zip<T2>(SQLList<T2> that)

Upvotes: 4

Related Questions