How should I think about Scala's Product classes?

Viewed 19284

The package "scala" has a number of classes named Product, Product1, Product2, and so on, up to Product22.

The descriptions of these classes are surely precise. For example:

Product4 is a cartesian product of 4 components

Precise, yes. Communicative? Not so much. I expect that this is the perfect wording for someone who already understands the sense of "cartesian product" being used here. For someone who doesn't, it sounds a bit circular. "Oh yes, well of course Product4 is the mumble product of 4 mumble-mumbles."

Please help me understand the correct functional-language viewpoint. What is the sense of "cartesian product" being used here? What do the Product classes' "projection" members indicate?

5 Answers

I think somebody might feel confused for Product just works like a member iterator, just like I did.

In fact, I think in 2019 everybody knows what a Cartesian Product is. But where is Cartesian Product in a Tuple ? I know if we have {a,b,c} and {1,2,3} we'll get {a,1},{a,2}...{c,3}. But when we come across Tuple2(a,1) we just have (a,1), how can one object Product?

So let's treat classes that implement Product as declarations. If class A(String, Int, Double) implentment Product3 , we treat the class as result of a Cartesian Product of (String, Int, Double), thus you know you can use _1 _2 _3method now.

Related