Skip to content
loading...

re: Universally quantified types in C# VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Your problem is interesting. It feels like it should work, but it doesn't. The question is really, why can't I convert IList<int> to IList&l...
 

Thanks for your response. Can you make it work with a generic getWeight (i.e. one that takes an IList<T>) and without using reflection? It can be done!

 

Hmm... there are multiple ways to do it. I don't like any of them. This might work:

private static int SumWeights(
            IList<int> ints, 
            IList<string> strs, 
            Func<dynamic,int> getWeight) 
            => getWeight(ints) + getWeight(strs);
...
Console.WriteLine(SumWeights(ints, strs, x=>getWeight1(x)));

You’re on the right track with your idea to insert something that prevents the generic-ness of getWeight from bubbling up to SumWeights. However, it can be done safely, without going around the compiler’s type checker via dynamic.

You can't pass a generic function as a parameter without having your own function be generic. Therefore you need to encapsulate it. Like in an interface. This would be the best design for your problem.

    interface IWeighter
    {
        int getWeight<T>(IList<T> list);
    }

But you want to pass a function as a parameter, so probably this ain't it. I have the feeling that whatever you're proposing will not sit well with me.

You got it. That interface is exactly what I'd suggest. You're still passing in the function, just with one level of indirection. It's a bit more verbose, but not too bad, I think. Nice work!

code of conduct - report abuse