{{Short description|Result of multiplying types in type theory}} {{more footnotes needed|date=October 2020}} In programming languages and type theory, a '''product''' of ''types'' is another, compounded, type in a structure. The "operands" of the product are types, and the structure of a product type is determined by the fixed order of the operands in the product. An instance of a product type retains the fixed order, but otherwise may contain all possible instances of its primitive data types. The expression of an instance of a product type will be a tuple, and is called a "tuple type" of expression. A product of types is a direct product of two or more types.

If there are only two component types, it can be called a "pair type". For example, if two component types <math>A</math> and <math>B</math> are the set of all possible values of that type, the product type written <math>A \times B</math> contains elements that are pairs <math>(a, b)</math>, where <math>a</math> and <math>b</math> are instances of <math>A</math> and <math>B</math> respectively. The pair type is a special case of the dependent pair type, where the type <math>B</math> may depend on the instance picked from <math>A</math>.

In many languages, product types take the form of a record type, for which the components of a tuple can be accessed by label. In languages that have algebraic data types, as in most functional programming languages, algebraic data types with one constructor are isomorphic to a product type.

In the Curry–Howard correspondence, product types are associated with logical conjunction ({{mono|AND}}) in logic.

The notion directly extends to the product of an arbitrary finite number of types (an <math>n</math>-ary product type), and in this case, it characterizes the expressions that behave as tuples of expressions of the corresponding types. A degenerate form of product type is the unit type: it is the product of no types.

In call-by-value programming languages, a product type can be interpreted as a set of pairs whose first component is a value in the first type and whose second component is a value in the second type. In short, it is a cartesian product and it corresponds to a product in the category of types.

Most functional programming languages have a primitive notion of product type. For instance, the product <math>T_1 \times T_2 \times ... \times T_n</math> is written <code>T<sub>1</sub> * T<sub>2</sub> * ... * T<sub>n</sub></code> in ML and <code>(T<sub>1</sub>, T<sub>2</sub>, ..., T<sub>n</sub>)</code> in Haskell. In both these languages, tuples are written <code>(v<sub>1</sub>, v<sub>2</sub>, ..., v<sub>n</sub>)</code> and the components of a tuple are extracted by pattern-matching. Additionally, many functional programming languages provide more general algebraic data types, which extend both product and sum types. Product types are the dual of sum types.

== Product types in programming languages == * C++ defines the class <code>std::tuple</code> (expressed <code>tuple<Ts...></code> using variadic templates),<ref>{{Cite web|title=std::tuple|url=https://en.cppreference.com/w/cpp/utility/tuple.html|website=cppreference.com|access-date=14 October 2025}}</ref> and for the specific case of two elements defines <code>std::pair</code> (expressed <code>pair<T, U></code>).<ref>{{Cite web|title=std::pair|url=https://en.cppreference.com/w/cpp/utility/pair.html|website=cppreference.com|access-date=14 October 2025}}</ref> <code>std::tuple</code> can be empty (<code>tuple<></code>). * C#/.NET Framework defines the class <code>System.Tuple</code>. There are specific instantiations for 1 to 8 elements. For the specific case of two elements (a pair), it uses <code>Tuple<T1, T2></code>.<ref>{{Cite web|title=Tuple Class|url=https://learn.microsoft.com/en-us/dotnet/api/system.tuple|website=learn.microsoft.com|publisher=Microsoft Learn|access-date=14 October 2025}}</ref> In order to create a tuple with nine or more components, the final parameter <code>TRest</code> of <code>Tuple<T1, T2, T3, T4, T5, T6, T7, TRest></code> is supplied as another tuple.<ref>{{Cite web|title=Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Class|url=https://learn.microsoft.com/en-us/dotnet/api/system.tuple-8|website=learn.microsoft.com|publisher=Microsoft Learn|access-date=14 October 2025}}</ref> For iterating over collections like dictionary types, the class <code>System.Collections.Generic.KeyValuePair</code> (expressed <code>KeyValuePair<TKey, TValue></code>) is provided.<ref>{{Cite web|title=KeyValuePair<TKey, TValue> struct|url=https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.keyvaluepair-2|website=learn.microsoft.com|publisher=Microsoft Learn|access-date=14 October 2025}}</ref> * Go does not have a tuple type, but can express multiple return values in a function as a sort of tuple.<ref>{{Cite web|title=Effective Go - Functions - Multiple return values|url=https://go.dev/doc/effective_go#multiple-returns|website=go.dev|access-date=14 October 2025}}</ref> * Haskell has a data type <code>Data.Tuple</code>.<ref>{{Cite web|title=Data.Tuple|url=https://hackage.haskell.org/package/base-4.21.0.0/docs/Data-Tuple.html|website=hackage.haskell.org|access-date=14 October 2025}}</ref> * Java does not have a general tuple type, but JavaFX has a type <code>javafx.util.Pair</code> (expressed <code>Pair<K, V></code>).<ref>{{Cite web|title=Class Pair<K, V>|url=https://download.java.net/java/GA/javafx25/docs/api/javafx.base/javafx/util/Pair.html|access-date=14 October 2025|website=download.java.net|publisher=Oracle Corporation}}</ref> For iterating over associative containers such as <code>java.util.Map</code>, a pair in the map is expressed as <code>Map.Entry<K, V></code>.<ref>{{Cite web|title=Interface Map.Entry<K, V>|url=https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Map.Entry.html|website=docs.oracle.com|access-date=14 October 2025|publisher=Oracle Corporation}}</ref> * Kotlin does not have a general tuple type, but has classes <code>kotlin.Pair</code> (expressed <code>Pair<A, B></code>)<ref>{{Cite web|title=Pair|url=https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-pair/|website=kotlinlang.org|access-date=14 October 2025|publisher=JetBrains s.r.o.}}</ref> and <code>kotlin.Triple</code> (expressed <code>Triple<A, B, C></code>).<ref>{{Cite web|title=Triple|url=https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-triple/|website=kotlinlang.org|access-date=14 October 2025|publisher=JetBrains s.r.o.}}</ref> * Python has a <code>tuple</code> collection which can be annotated as <code>typing.Tuple</code> (expressed <code>Tuple[T<sub>1</sub>, T<sub>2</sub>, ..., T<sub>N</sub>]</code>).<ref>{{Cite web|title=5. Data Structures|url=https://docs.python.org/3/tutorial/datastructures.html|website=docs.python.org|access-date=14 October 2025}}</ref> * Rust defines the primitive tuple type, expressed as <code>(T<sub>1</sub>, T<sub>2</sub>, ..., T<sub>N</sub>)</code>, and a pair is just <code>(T, U)</code>.<ref>{{Cite web|title=tuple - Rust|url=https://doc.rust-lang.org/std/primitive.tuple.html|website=doc.rust-lang.org|access-date=14 October 2025}}</ref> * Scala defines the class <code>scala.Tuple</code>,<ref>{{Cite web|title=Tuple|url=https://www.scala-lang.org/api/current/scala/Tuple.html|website=www.scala-lang.org|access-date=14 October 2025}}</ref> which supports between 2 and 22 objects as <code>scala.Tuple2</code> (expressed as <code>Tuple2[A, B]</code>)<ref>{{Cite web|title=Tuple2|url=https://www.scala-lang.org/api/current/scala/Tuple2.html|website=www.scala-lang.org|access-date=14 October 2025}}</ref> to <code>scala.Tuple22</code> (expressed as <code>Tuple22[A, B, ..., V]</code>).<ref>{{Cite web|title=Tuple22|url=https://www.scala-lang.org/api/current/scala/Tuple22.html|website=www.scala-lang.org|access-date=14 October 2025}}</ref> * Swift expresses tuples as <code>(T<sub>1</sub>, T<sub>2</sub>, ..., T<sub>N</sub>)</code>.<ref>{{Cite web|title=Types Documentation|url=https://docs.swift.org/swift-book/documentation/the-swift-programming-language/types/|website=docs.swift.org|publisher=Apple Inc.|access-date=14 October 2025}}</ref>

==See also== * Product (category theory) * Cartesian product * Record (computer science) * Struct (C programming language) * Sum type * Quotient type

==References== * {{nlab|id=product+type|title=product type}} * [http://homotopytypetheory.org/2013/06/20/the-hott-book/ ''Homotopy Type Theory: Univalent Foundations of Mathematics'', The Univalent Foundations Program, Institute for Advanced Study]. ''See section 1.5''. {{Reflist}}

{{data types}}

{{DEFAULTSORT:Product Type}} Category:Data types Category:Type theory Category:Composite data types