4 ms·
I don't know much about type theory, so I suspect that my intuition is wrong, but the phrase "intersection type" violates my intuition. If I have Printable and
by matchu 11y ago
I don't know much about type theory, so I suspect that my intuition is wrong, but the phrase "intersection type" violates my intuition.
If I have Printable and Drawable interfaces, which each describe a set of behaviors, then I would expect the "intersection" of Printable and Drawable to be the intersection of those sets of behaviors — which, here, is likely the empty set.
Is there another way of understanding types that makes the word "intersection" make more sense here? Or is it just to say that "where union types say OR, intersection types say AND"?
---
...ya know what? By typing it out, I think I get it now.
The set that we're talking about isn't the set of Printable's behaviors; it's the set of things that implement Printable. `Printable & Drawable` is the intersection of the set of types that implement Printable with the set of types that implement Drawable. Got it. Thanks for rubber-ducking for me, HN!
- spicyj 11y agoObjects whose type is a subtype of "Printable | Drawable" (union) are those that implement either interface. For "Printable & Drawable" (intersection), you have objects that implement both interfaces. Meaning, those objects that have both .print() and .draw() (or similar). You're not taking the intersection of the interfaces, but of the sets of objects that implement them.
- CurtHagenlocher 11y agoI found some of the comments at http://stackoverflow.com/questions/5653678/union-types-and-intersection-types http://stackoverflow.com/questions/5653678/union-types-and-i... helpful. If TAPL refers to "intersection types" then it's definitely a well-defined concept.
- zeckalpha 11y agoThere's also sum and product types, again by analogy.
- ken47 11y agoIt's not the same sort of intersection as you might understand it from mathematical sets: http://stackoverflow.com/questions/5653678/union-types-and-intersection-types http://stackoverflow.com/questions/5653678/union-types-and-i... See the response about C#.
- WorldMaker 11y agoIt is referring to mathematical sets, but at the categorization of types level rather than the "shape" of types. If you think of Set X consisting of all the objects that could possibly be built as Class X and Set Y as all the objects that could possibly be built as Class Y, then the union of those sets is all of the Class X derived objects and all of the Class Y derived objects, whereas the intersection of those sets are whatever objects manage to implement or derive from both Class X and Class Y.