Headline

A means of constraining a polymorphic type

Illustration

The notion of type constraint is pretty general as it occurs in different ways in programming languages which is why we do not attempt a comprehensive description here. Instead, we briefly compare how type constraints show up in Java versus Haskell.

We use the following problem to illustrate type constraints: Suppose you want to count the elements in an array that are greater than a given element. This sort of function (or method) would need to be parametric in the element type of the array and that type would also need to be constrained to admit comparison.

Type constraints in Java

The type signature for a suitable method for the problem at hand is of the following form:

public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem)

Type constraints in Java

The type signature for a suitable function for the problem at hand is of the following form:

countGreaterThan :: Ord t => [t] -> t -> Ordering

Haskell's type constraints are discussed in more detail in the broader context of type signatures.

Metadata


Marcel Heinz edited this article at Wed, 31 Jan 2018 12:18:32 +0100
Compare revisions Compare revisions

User contributions

    This user never has never made submissions.

    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.

    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.