Lably
Skip to content

Type signature

Defines the inputs and outputs for a function, subroutine or method

  • 8 min read
  • 3 chapters

Related on Lably

Through Computer science, Function (computer programming) and Method (computer programming)

Examples

C/C++

In C and C++, the type signature is declared by what is commonly known as a function prototype. In C/C++, a function declaration reflects its use; for example, a function pointer with the signature (int)(char, double) would be called as:

<span id="mwJw"></span><span class="kt" id="mwKA">char</span><span class="w" id="mwKQ"> </span><span class="n" id="mwKg">c</span><span class="p" id="mwKw">;</span> <span class="kt" id="mwLA">double</span><span class="w" id="mwLQ"> </span><span class="n" id="mwLg">d</span><span class="p" id="mwLw">;</span> <span class="kt" id="mwMA">int</span><span class="w" id="mwMQ"> </span><span class="n" id="mwMg">retVal</span><span class="w" id="mwMw"> </span><span class="o" id="mwNA">=</span><span class="w" id="mwNQ"> </span><span class="p" id="mwNg">(</span><span class="o" id="mwNw">*</span><span class="n" id="mwOA">fPtr</span><span class="p" id="mwOQ">)(</span><span class="n" id="mwOg">c</span><span class="p" id="mwOw">,</span><span class="w" id="mwPA"> </span><span class="n" id="mwPQ">d</span><span class="p" id="mwPg">);</span>

Erlang

In Erlang, type signatures may be optionally declared, as:

<span id="mwSA"></span><span class="p" id="mwSQ">-</span><span class="ni" id="mwSg">spec</span><span class="w" id="mwSw"> </span><span class="n" id="mwTA">function_name</span><span class="p" id="mwTQ">(</span><span class="n" id="mwTg">type1</span><span class="p" id="mwTw">(),</span><span class="w" id="mwUA"> </span><span class="n" id="mwUQ">type2</span><span class="p" id="mwUg">(),</span><span class="w" id="mwUw"> </span><span class="p" id="mwVA">...)</span><span class="w" id="mwVQ"> </span><span class="o" id="mwVg">-></span><span class="w" id="mwVw"> </span><span class="n" id="mwWA">out_type</span><span class="p" id="mwWQ">().</span>

For example:

<span id="mwXQ"></span><span class="p" id="mwXg">-</span><span class="ni" id="mwXw">spec</span><span class="w" id="mwYA"> </span><span class="n" id="mwYQ">is_even</span><span class="p" id="mwYg">(</span><span class="n" id="mwYw">number</span><span class="p" id="mwZA">())</span><span class="w" id="mwZQ"> </span><span class="o" id="mwZg">-></span><span class="w" id="mwZw"> </span><span class="n" id="mwaA">boolean</span><span class="p" id="mwaQ">().</span>

Haskell

A type signature in Haskell generally takes the following form:

<span id="mwbw"></span><span class="nf" id="mwcA">functionName</span><span class="w" id="mwcQ"> </span><span class="ow" id="mwcg">::</span><span class="w" id="mwcw"> </span><span class="n" id="mwdA">arg1Type</span><span class="w" id="mwdQ"> </span><span class="ow" id="mwdg">-></span><span class="w" id="mwdw"> </span><span class="n" id="mweA">arg2Type</span><span class="w" id="mweQ"> </span><span class="ow" id="mweg">-></span><span class="w" id="mwew"> </span><span class="o" id="mwfA">...</span><span class="w" id="mwfQ"> </span><span class="ow" id="mwfg">-></span><span class="w" id="mwfw"> </span><span class="n" id="mwgA">argNType</span>

Notice that the type of the result can be regarded as everything past the first supplied argument. This is a consequence of currying, which is made possible by Haskell's support for first-class functions; this function requires two inputs where one argument is supplied and the function is "curried" to produce a function for the argument not supplied. Thus, calling f x, where f :: a -> b -> c, yields a new function f2 :: b -> c that can be called f2 y to produce c.

The actual type specifications can consist of an actual type, such as Integer, or a general type variable that is used in parametric polymorphic functions, such as a, or b, or anyType. So we can write something like: functionName :: a -> a -> ... -> a

Since Haskell supports higher-order functions, functions can be passed as arguments. This is written as: functionName :: (a -> a) -> a

This function takes in a function with type signature a -> a and returns data of type a out.

Java

In the Java virtual machine, internal type signatures are used to identify methods and classes at the level of the virtual machine code.

Example: The method String String.substring(int, int) is represented in bytecode as Ljava/lang/String.substring(II)Ljava/lang/String; .

The signature of the main method looks like this:

<span id="mwARk"></span><span class="kd" id="mwARo">public</span><span class="w" id="mwARs"> </span><span class="kd" id="mwARw">static</span><span class="w" id="mwAR0"> </span><span class="kt" id="mwAR4">void</span><span class="w" id="mwAR8"> </span><span class="nf" id="mwASA">main</span><span class="p" id="mwASE">(</span><span class="n" id="mwASI">String</span><span class="o" id="mwASM">[]</span><span class="w" id="mwASQ"> </span><span class="n" id="mwASU">args</span><span class="p" id="mwASY">);</span>

And in the disassembled bytecode, it takes the form of Lsome/package/Main/main:([Ljava/lang/String;)V

The method signature for the main() method contains three modifiers:

  • public indicates that the main() method can be called by any object.
  • static indicates that the main() method is a class method.
  • void indicates that the main() method has no return value.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication