Skip to content

re: Universally quantified types in C# VIEW POST

re: Why would you try to introduce another problem and pull code from a different domain? // getWeight() is a method specific to the Interface IList&...

What if there were multiple different versions of getWeight, and you wanted to SumWeights to work with any of them? You'd have to pass it in somehow as an argument, right?


Aren't you contradicting yourself? Shouldn't pure function always return the same result with the same input? What exactly you mean by "multiple different getWeight() functions"? Each domain implements only one existing version from the Interface. There can be differences but only in input parameters to the getWeight() method which are handled by the compiler inference.

You said you need to sum the weights. While it is possible to sum int and float by doing implicit conversion, there cannot exist implementation of an interface method with different output type.

While what you propose introduces domain modelling problem. Imagine a new developer tasked to implement IList<float>. When he's finished implementing the interface your code is still broken because he has no idea about your Func<IList<>, int> getWeight() method where he needs to additionally implement new case/switch to handle float sums.

I think you're making this more complex than it needs to be. I'm just trying to create a scenario where you're passing a generic function (getWeight) to a non-generic method (SumWeights).

Are you talking about IoC / DI? ;)

Edit: Now I remember! Your solution reminds me of Service locator pattern, which is BTW an anti-pattern. That's why I found it immediately wrong.

code of conduct - report abuse