Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Product type

From Wikipedia, the free encyclopedia
Result of multiplying types in type theory
This article includes a list ofgeneral references, butit lacks sufficient correspondinginline citations. Please help toimprove this article byintroducing more precise citations.(October 2020) (Learn how and when to remove this message)

Inprogramming languages andtype theory, aproduct oftypes is another, compounded, type in a structure. The "operands" of the product aretypes, 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 itsprimitive data types. The expression of an instance of a product type will be atuple, and is called a "tuple type" of expression. A product of types is adirect 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 typesA{\displaystyle A} andB{\displaystyle B} are the set of all possible values of that type, the product type writtenA×B{\displaystyle A\times B} contains elements that are pairs(a,b){\displaystyle (a,b)}, wherea{\displaystyle a} andb{\displaystyle b} are instances ofA{\displaystyle A} andB{\displaystyle B} respectively. The pair type is a special case of thedependent pair type, where the typeB{\displaystyle B} may depend on the instance picked fromA{\displaystyle A}.

In many languages, product types take the form of arecord type, for which the components of a tuple can be accessed bylabel. In languages that havealgebraic data types, as in mostfunctional programming languages, algebraic data types with one constructor areisomorphic to a product type.

In theCurry–Howard correspondence, product types are associated withlogical conjunction (AND) inlogic.

The notion directly extends to the product of an arbitrary finite number of types (ann{\displaystyle n}-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 theunit type: it is theproduct of no types.

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

Mostfunctional programming languages have aprimitive notion of product type. For instance, the productT1×T2×...×Tn{\displaystyle T_{1}\times T_{2}\times ...\times T_{n}} is writtenT1 * T2 * ... * Tn inML and(T1, T2, ..., Tn) inHaskell. In both these languages, tuples are written(v1, v2, ..., vn) and the components of a tuple are extracted bypattern-matching. Additionally, many functional programming languages provide more generalalgebraic data types, which extend both product andsum types. Product types are thedual of sum types.

Product types in programming languages

[edit]
  • C++ defines the classstd::tuple (expressedtuple<Ts...> usingvariadic templates),[1] and for the specific case of two elements definesstd::pair (expressedpair<T, U>).[2]std::tuple can be empty (tuple<>).
  • C#/.NET Framework defines the classSystem.Tuple. There are specific instantiations for 1 to 8 elements. For the specific case of two elements (a pair), it usesTuple<T1, T2>.[3] In order to create a tuple with nine or more components, the final parameterTRest ofTuple<T1, T2, T3, T4, T5, T6, T7, TRest> is supplied as another tuple.[4] For iterating over collections like dictionary types, the classSystem.Collections.Generic.KeyValuePair (expressedKeyValuePair<TKey, TValue>) is provided.[5]
  • Go does not have a tuple type, but can express multiple return values in a function as a sort of tuple.[6]
  • Haskell has a data typeData.Tuple.[7]
  • Java does not have a general tuple type, butJavaFX has a typejavafx.util.Pair (expressedPair<K, V>).[8] For iterating over associative containers such asjava.util.Map, a pair in the map is expressed asMap.Entry<K, V>.[9]
  • Kotlin does not have a general tuple type, but has classeskotlin.Pair (expressedPair<A, B>)[10] andkotlin.Triple (expressedTriple<A, B, C>).[11]
  • Python has atuple collection which can be annotated astyping.Tuple (expressedTuple[T1, T2, ..., TN]).[12]
  • Rust defines the primitive tuple type, expressed as(T1, T2, ..., TN), and a pair is just(T, U).[13]
  • Scala defines the classscala.Tuple,[14] which supports between 2 and 22 objects asscala.Tuple2 (expressed asTuple2[A, B])[15] toscala.Tuple22 (expressed asTuple22[A, B, ..., V]).[16]
  • Swift expresses tuples as(T1, T2, ..., TN).[17]

See also

[edit]

References

[edit]
  1. ^"std::tuple".cppreference.com. Retrieved14 October 2025.
  2. ^"std::pair".cppreference.com. Retrieved14 October 2025.
  3. ^"Tuple Class".learn.microsoft.com. Microsoft Learn. Retrieved14 October 2025.
  4. ^"Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Class".learn.microsoft.com. Microsoft Learn. Retrieved14 October 2025.
  5. ^"KeyValuePair<TKey, TValue> struct".learn.microsoft.com. Microsoft Learn. Retrieved14 October 2025.
  6. ^"Effective Go - Functions - Multiple return values".go.dev. Retrieved14 October 2025.
  7. ^"Data.Tuple".hackage.haskell.org. Retrieved14 October 2025.
  8. ^"Class Pair<K, V>".download.java.net. Oracle Corporation. Retrieved14 October 2025.
  9. ^"Interface Map.Entry<K, V>".docs.oracle.com. Oracle Corporation. Retrieved14 October 2025.
  10. ^"Pair".kotlinlang.org. JetBrains s.r.o. Retrieved14 October 2025.
  11. ^"Triple".kotlinlang.org. JetBrains s.r.o. Retrieved14 October 2025.
  12. ^"5. Data Structures".docs.python.org. Retrieved14 October 2025.
  13. ^"tuple - Rust".doc.rust-lang.org. Retrieved14 October 2025.
  14. ^"Tuple".www.scala-lang.org. Retrieved14 October 2025.
  15. ^"Tuple2".www.scala-lang.org. Retrieved14 October 2025.
  16. ^"Tuple22".www.scala-lang.org. Retrieved14 October 2025.
  17. ^"Types Documentation".docs.swift.org. Apple Inc. Retrieved14 October 2025.
Uninterpreted
Numeric
Pointer
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Product_type&oldid=1322824525"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp