Scala | Product3

Product3 is a trait in Scala, which is a Cartesian product of three elements. The Linear Supertypes here are Product, Equals, and Any and the known subclass here is Tuple3. It extends the trait Product i.e,

`trait Product3[+T1, +T2, +T3] extends Product`

Where, T1, T2, and T3 are the used parameter types of the Scala.

1. Abstract Value Members
The abstract value members here are:

• `abstract def _1: T1`

It returns the elements of the first parameter type.

• `abstract def _2: T2`

It returns the elements of the second parameter type.

• `abstract def _3: T3`

It returns the elements of the third parameter type.

• `abstract def canEqual(that: Any): Boolean`

It returns true if two instances are equal else returns false.

Example :

 `// Scala program of a trait ` `// Product3 ` ` `  `// Creating an object ` `object` `GfG ` `{ ` ` `  `    ``// Main method ` `    ``def` `main(args``:` `Array[String])  ` `    ``{ ` `     `  `        ``// Applying Produt3 trait ` `        ``// and assigning values ` `        ``val` `x``:` `Product``3``[String, Int, Double] ``=` `        ``{ ` `            ``(``"GeeksforGeeks"``, ``32``, ``43``) ` `        ``} ` `        ``// Displays the first element ` `        ``println(x.``_``1``) ` `     `  `        ``// Displays the second element ` `        ``println(x.``_``2``) ` `     `  `        ``// Displays the third element ` `        ``println(x.``_``3``) ` `             `  `    ``} ` `} `

Output:

```GeeksforGeeks
32
43.0
```

Here, we have utilized abstract value members for accessing the elements.

2. Concrete Value Members
The concrete value members here are:

• `def productArity: Int`

It returns the number of parameters in Product3 trait.

• `def productElement(n: Int): Any`

It returns nth element.

• `def productIterator: Iterator[Any]`

It returns an iterator by default.

• `def productPrefix: String`

It returns the empty string by default.

Example :

 `// Scala program of a trait ` `// Product3 for concrete  ` `// value members ` ` `  `// Creating an object ` `object` `GfG ` `{ ` ` `  `    ``// Main method ` `    ``def` `main(args``:` `Array[String])  ` `    ``{ ` `     `  `        ``// Applying Product3 trait ` `        ``// and assigning values ` `        ``val` `x``:` `Product``3``[String, Char, Int] ``=` `        ``{ ` `            ``(``"GeeksforGeeks"``, ``'a'``, ``43``) ` `        ``} ` `         `  `        ``// Displays number of elements ` `        ``println(x.productArity) ` `     `  `        ``// Displays nth element ` `        ``println(x.productElement(``1``)) ` `     `  `        ``// Displays prefix of the trait ` `        ``println(x.productPrefix) ` `             `  `    ``} ` `} `

Output:

```3
a
Tuple3
```

Here, productPrefix will return Tuple3 as Tuple3 is a final case class that extents Product3.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.