DEV Community

Discussion on: Universally quantified types in C#

Collapse
 
slavius profile image
Slavius • Edited

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.

Thread Thread
 
shimmer profile image
Brian Berns

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).

Thread Thread
 
slavius profile image
Slavius • Edited

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.