# Why is there a type "Pair"?

**URL:** https://discourse.julialang.org/t/why-is-there-a-type-pair/51698
**Category:** New to Julia
**Created:** [December 12, 2020, 12:56am UTC](https://discourse.julialang.org/t/why-is-there-a-type-pair/51698 "2020-12-12T00:56:32Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [December 12, 2020, 12:56am UTC](https://discourse.julialang.org/t/why-is-there-a-type-pair/51698/1 "2020-12-12T00:56:32Z")

</div>

I recently learned of the existence of the =\> operator, which forms an element of type Pair:

```julia
julia> 2 => ones(4)
2 => [1.0, 1.0, 1.0, 1.0]

```

which makes a Pair. I just assumed Pair was a convenience type synonym for a length 2 Tuple, but it’s not.

```julia
julia> typeof((1,2)) == typeof(1 => 2)
false

```

Just curious as to what functionality Pair provides that a two element tuple could not ?

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [December 12, 2020, 1:03am UTC](https://discourse.julialang.org/t/why-is-there-a-type-pair/51698/2 "2020-12-12T01:03:28Z")

</div>

A 2-tuple is a collection of elements that may have nothing to do with each other; a pair associates the first element with the second. It’s typically used to relate dictionary keys with their respective values, for instance.

Regarding functionality differences, as their docstring tell, a Pair is treated as a single “scalar” for broadcasting operations.

---

<div class="post-metadata">

### Author: ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)
#### Post date: [December 12, 2020, 1:05am UTC](https://discourse.julialang.org/t/why-is-there-a-type-pair/51698/3 "2020-12-12T01:05:42Z")

</div>

The reason we need a difference is to facilitate broadcast. Broacasting over `(1,2)` applies to the two elements separately, but broadcasting treats `1=>2` as a scalar.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 12, 2020, 2:23am UTC](https://discourse.julialang.org/t/why-is-there-a-type-pair/51698/4 "2020-12-12T02:23:24Z")

</div>

> [@malacroi](#):
>
> The reason we need a difference is to facilitate broadcast.

No, the `Pair` type came long before the dot syntax for broadcasting.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [December 12, 2020, 9:39am UTC](https://discourse.julialang.org/t/why-is-there-a-type-pair/51698/5 "2020-12-12T09:39:26Z")

</div>

See here for a previous (short) discussion: [https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/](https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 12, 2020, 2:34pm UTC](https://discourse.julialang.org/t/why-is-there-a-type-pair/51698/6 "2020-12-12T14:34:46Z")

</div>

> [@purplishrock](#):
>
> Just curious as to what functionality Pair provides that a two element tuple could not ?

That’s not the right question in Julia. _All_ 2-element collections _could_ do the same thing.

But, of course, you may not _want_ them to do the same thing, and having a different type allows different semantics using dispatch. Specifically, `key => value` is used extensively in Julia.

> [@purplishrock](#):
>
> I just assumed Pair was a convenience type synonym for a length 2 Tuple

Generally those are done using a [type alias](https://docs.julialang.org/en/v1/manual/types/#Type-Aliases).

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [December 12, 2020, 3:23pm UTC](https://discourse.julialang.org/t/why-is-there-a-type-pair/51698/7 "2020-12-12T15:23:09Z")

</div>

> [@Tamas\_Papp](#):
>
> _All_ 2-element collections _could_ do the same thing.

_cough_ arrays _cough_

---

<div class="post-metadata">

### Author: ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)
#### Post date: [December 12, 2020, 5:56pm UTC](https://discourse.julialang.org/t/why-is-there-a-type-pair/51698/8 "2020-12-12T17:56:34Z")

</div>

Of course pairs and 2-tuples are structurally the same, and 2-tuples can just as well represent an association from the first item to the second item (that is the definition of a mathematical functional relation after all: [[1]](https://en.wikipedia.org/wiki/Function_(mathematics)#Relational_approach), [[2]](https://en.wikipedia.org/wiki/Binary_relation#Special_types_of_binary_relations), [[3]](https://en.wikipedia.org/wiki/Ordered_pair), [[4]](https://encyclopediaofmath.org/wiki/Function), [[5]](https://encyclopediaofmath.org/wiki/Functional_relation), [[6]](https://encyclopediaofmath.org/wiki/Binary_relation)).

The reason for the distinction is to clarify our intentions about using them: you use `Pair` when you want to represent a functional relation, you use `Tuple` when you want an ordered container. This way you reduce the chances of messing things up inadvertently.

It is same reason why you might want to introduce two structurally equivalent types

```julia
struct Meters{R<:Real} val::R end
struct Kilograms{R<:Real} val::R end

```

just so that you cannot accidentally confuse them and add `Meters(3.14) + Kilograms(42.)`.

The _newtype_ pattern ([[7]](https://wiki.haskell.org/Newtype)) is based on this principle: you want a type which has the same underlying representation, but can behave differently.
