Headline

The type associated with an identifier

Synonyms

Other terms may be used for referring to type signatures in the context of specific programming paradigms and programming languages, e.g.:

Description

Type signatures in functional programming

All expressions and all named functions for that matter have an associated type. When such a type is explicitly assigned (declared), then we speak of a type signature. When the type system of the language at hand permits inference, as in the case of Language:Haskell, for example, then the type signature may also be omitted; one may nevertheless infer the type of the defined function.

Illustration

Type signatures in Haskell

We investigate some type signatures of predefined functions at the Haskell prompt. To this end, we use the interpreter command :t" to ask for types of expressions. (User input at the Haskell prompt is prefixed by ">"; the result is shown in the subsequent line(s).)

Here is the type signature of Boolean negation.

> :t not
not :: Bool -> Bool

The type signature declares that the function not takes an argument of type Bool and returns a result Bool.

Hre is the type signature of conjunction ("&&").

> :t (&&)
(&&) :: Bool -> Bool -> Bool

By enclosing "&&" into parentheses, we deal with the fact that "&&" is an infix operator. The type signature declares that the function "&&" takes two arguments of type Bool and returns a result of type Bool.

Types and function signatures for that matter may be polymorphic; this can be seen from the occurrence of type variables (starting in lower case). Let's look at some examples at the Haskell prompt.

The totally undefined function is of a completely polymorphic type.

> :t undefined
undefined :: a

The identity function (id) is polymorphic with the same type variable for domain and range.

> :t id
id :: a -> a

The projection function for the first component of a pair (fst) is polymorphic in the type of the two components of the pair; the result is of the same type as the first component.

> :t fst
fst :: (a, b) -> a

The concatenation function ("++") is polymorphic in the element type of the lists to be concatenated. That is, the function takes two lists of the same polymorphic type and returns a list of the same polymorphic type.

> :t (++)
(++) :: [a] -> [a] -> [a]

In Haskell, type variables in type signatures may be constrained so that the variables are not universally polymorphic, but they can only be instantiated with types that are instances of appropriate type classes (i.e., sets of types). Without going into detail here, let's look at some examples. Constraints precede argument and result types before an extra arrow "=>".

Equality is not universally polymorphic; it can be applied only to types which define equality; this is modeled by a type class (set of types) Eq. For instance, strings can be compared for equality, but functions cannot.

> :t (==)
(==) :: Eq a => a -> a -> Bool
> "foo" == "bar"
False
> not == not
... error ... no instance for (Eq (Bool -> Bool)) ...

There are several number types (in Haskell). Thus, addition ("+") is overloaded on the grounds of the following signature which refers to a type class Num; we also show addition for two different number types: integer (Int) and floating point numbers (Float).

> :t (+)
(+) :: Num a => a -> a -> a
> 20.9+21.1
42.0
> 21+21
42

Method signatures in Java

A static method in Language:Java for computing the absolute value of a float may have the following type signature:

public static float abs(float a)

That is, the method takes an argument of type float for the value to be mapped to its absolute value. Further, the method's result type is float for the actual absolute value.

An instance method in Language:Java for withdrawing money from an account may have the following type signature:

public boolean withdraw(float amount)

That is, the method takes an argument of type float for the amount to be withdrawn. Further, the method's result type is boolean to be able to communicate whether withdrawal was successful or not.

Citation

(http://en.wikipedia.org/wiki/Type_signature, 21 April 2013)

In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes at least the function name and the number of its arguments. In some programming languages, it may also specify the function's return type, the types of its arguments, or errors it may pass back.


There are no revisions for this page.

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.