## Concept:

# Either type

## Headline

A type for disjoint (indexed) sums over types

## Illustration

We illustrate here the Haskell approach to either types.

The corresponding polymorphic type type constructor is defined as follows:

```
data Either a b = Left a | Right b
```

Thus, a value of an either type is either of one type or another and the choice is also conveyed by the constructors *Left* versus *Right*. One typical application scenario is error handling where one argument type models error messages (e.g., String) and the other argument type models successful results. In this instance, either types generalize maybe types.

Another typical application scenario is mixed-type computations. For instance, assume that we have some mathematical operations that may return both *Int* and *Float*. Here is a corresponding either type:

```
type IntOrFloat = Either Int Float
```

As an example of a function that needs to manipulate values of the either type, consider the following function that extracts a *Float* by applying the conversion *fromIntegral* if given an *Int*:

```
asFloat :: IntOrFloat -> Float
asFloat (Left x) = fromIntegral x
asFloat (Right x) = x
```

For instance:

```
> asFloat (Left 42)
42.0
> asFloat (Right 42.0)
42.0
```

Because case discrimination on an either type is so common, there is even (in Haskell) a standard higher-order function by which the same conversion can be expressed more concisely:

```
asFloat :: IntOrFloat -> Float
asFloat = either fromIntegral id
```

Specific either types can also be expressed by other means than a designated type constructor for such types. For instance, in functional programming with algebraic data types, a specific type can be declared for a given sum. For instance, the sum over *Int* and *Float* could also be declared like this:

```
data IntOrFloat = Int Int | Float Float
```

(We reuse type names as constructor symbols here, which is possible in Haskell, as these are separate namespaces.) The earlier conversion function is now to be defined by ordinary case discrimination over a (non-polymorphic) algebraic data type:

```
asFloat :: IntOrFloat -> Float
asFloat (Int x) = fromIntegral x
asFloat (Float x) = x
```

The advantage of the either type constructor is that it captures universally (polymorphically) the notion of disjoint (labeled) sum. Clearly, sums with more than two cases can be expressed by nested applications of the type constructor.

## Metadata

### There are no revisions for this page.

## User contributions

## User edits

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**

#### 10 most similar pages:

- Concept:Tuple type - 1.1360595
- Concept:Type class - 0.84451985
- Concept:Algebraic data type - 0.6460725
- Concept:Data modeling - 0.6216981
- Concept:Fold_function - 0.57900614
- Contribution:haskellScott - 0.5772178
- Concept:Type class Ord - 0.5528969
- Concept:Scrap your boilerplate - 0.5491574
- Concept:Foldable type - 0.52518773
- Concept:Pattern matching - 0.5171432

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**