4 ms·
> The classic example is that there’s an infinite number of functions with type int -> int But are they really int to begin with, as in actually ratio values?
by etripe 6y ago
> The classic example is that there’s an infinite number of functions with type int -> int
But are they really int to begin with, as in actually ratio values? Perhaps you're inputting the units requested and getting units available back, which will be completely different from a function that takes an int for the items currently in the basket and returns a new total of items in the basket. Can you compose all those functions into each other? If not, they're not really using the same type but you're representing them as such. That would be undesired behaviour you can totally avoid through subtypes StockAmount and BasketItems.
Edit: If you're talking about the math domain, obviously subtyping doesn't apply.
- bluGill 6y agoUnfortunately most languages (not all!) don't have a good subtype system. In C++ (or python) I can create a StockAmount class, but since this behaves like an int I have to write something like 30 operators by hand, they all have an obvious implementation but there isn't a good way to get them automatically. The I have to do the same thing with BasketItems. At least C++ lets you make a class that looks like an int, I don't know how you would do this in C, even Java doesn't have operator+ (or didn't last time I used it) I've been thinking about writing a proposal for C++. Problem isn't easy to find something worth writing. Either it is obviously too simple to fit my needs, or it is so complex that I wouldn't accept it if I were on the committee - and this the thought exercise stage where true rigor of reasoning hasn't been applied.