How should I think about Scala's Product classes?

前端 未结 5 1523
情深已故
情深已故 2020-12-07 22:19

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

5条回答
  •  天涯浪人
    2020-12-07 23:09

    "The set of all possible pairs of elements whose components are members of two sets."

    "Specifically, the Cartesian product of two sets X (for example the points on an x-axis) and Y (for example the points on a y-axis), denoted X × Y, is the set of all possible ordered pairs whose first component is a member of X and whose second component is a member of Y (e.g. the whole of the x-y plane)"

    Perhaps better understanding can be gained by knowing who derives from it:

    Direct Known Subclasses: Tuple4

    Or by, knowing it "extends Product", know what other classes can make use of it, by virtue of extending Product itself. I won't quote that here, though, because it's rather long.

    Anyway, if you have types A, B, C and D, then Product4[A,B,C,D] is a class whose instances are all possible elements of the cartesian product of A, B, C and D. Literally.

    Except, of course, that Product4 is a Trait, not a class. It just provides a few useful methods for classes that are cartesian products of four different sets.

提交回复
热议问题