2 ms·
Generic container types should really have two parameters: one type that you're guaranteed to be able to put in, and one that you're guaranteed to get when you
by gjm11 15y ago
Generic container types should really have two parameters: one type that you're guaranteed to be able to put in, and one that you're guaranteed to get when you take it out. Then a Container<emits:A,accepts:B> is-a Container<emits:C,accepts:D> if A<C and B>D.
So, e.g., HarvestTomatoes() can return a Basket<Tomato>, which is shorthand for Basket<emits:Tomato,accepts:Tomato>, and MakeJuice can take a Basket<emits:Fruit,accepts:EMPTY_TYPE>, and it's safe to feed the former into something that wants the latter.
(Of course actual container objects will typically be of type Container<emits:T,accepts:T> for some specific T.)