## 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.118961
- Concept:Type class - 0.8480757
- Concept:Algebraic data type - 0.6316054
- Contribution:haskellScott - 0.57161903
- Concept:Fold_function - 0.56662947
- Concept:Type class Ord - 0.5464837
- Concept:Scrap your boilerplate - 0.5434512
- Concept:Foldable type - 0.5248851
- Concept:Fold algebra - 0.5116477
- Technology:Parsec - 0.50688815

## 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.
**