In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes the number, types and order of the arguments contained by a function. A type signature is typically used during overload resolution for choosing the correct definition of a function to be called among many overloaded forms.
In Erlang, type signatures may be optionally declared, as: -spec, type2,...) -> out_type).
For example: -spec) -> boolean).
Haskell
A type signature in the Haskell programming language is generally written in the following format: functionName :: arg1Type -> arg2Type ->... -> argNType
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 supplied and the function is "curried" to produce a function for the argument not supplied. Thus calling, where, yields a new function that can be called to produce. The actual type specifications can consist of an actual type, such as, or a general type variable that is used in parametric polymorphicfunctions, such as, or, or. So we can write something like: Since Haskell supports higher-order functions, functions can be passed as arguments. This is written as: This function takes in a function with type signature and returns data of type 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 is represented in bytecode as. The signature of main method looks like this: public static void main
And in the disassembled bytecode, it takes the form of 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.
Signature
A function signature consists of the function prototype. It specifies the general information about a function like the name, scope and parameters. Many programming languages use name mangling in order to pass along more semantic information from the compilers to the linkers. In addition to mangling, there is an excess of information in a function signature which is not readily available, but may be accessed. Understanding the notion of a function signature is an important concept for all computer science studies.
Modern object orientation techniques make use of interfaces, which are essentially templates made from function signatures.
The practice of multiple inheritance requires consideration of the function signatures to avoid unpredictable results. Computer science theory, and the concept of polymorphism in particular, make much use of the concept of function signature. In the C programming language signature is roughly equivalent to its prototype definition. The term "signature" may carry other meanings in computer science. For example:
File signatures can identify or verify the content of a file.
Database signatures can identify or verify the schema or a version of a database.
In the ML family of programming languages, "signature" is used as a keyword referring to a construct of the module system that plays the role of an interface.
Method signature
In computer programming, especially object-oriented programming, a method is commonly identified by its unique method signature, which usually includes the method name, and the number, types and order of its parameters. A method signature is the smallest type of a method.
Examples
C/C++
In C/C++, the method signature is the method name and the number and type of its parameters, but it is possible to have a last parameter that consists of an array of values: int printf;
Manipulation of these parameters can be done by using the routines in the standard library header stdarg.h|.
Similar to the C syntax, C# sees as the method signature its name and the number and type of its parameters, where the last parameter may be an array of values: void Add; Add; // sum 25
Java
In the Java programming language, a method signature is the method name and the number, type and order of its parameters. Return types and thrown exceptions are not considered to be a part of the method signature. methodName ; break;
For example, the following two methods have distinct signatures: doSomething; doSomething;
The following three methods do have the same signatures and are considered the same, as only the return value differs. The name of the parameter is not part of the method signature and is ignored by the compiler for checking method uniqueness. int doSomething String doSomething int doSomething throws java.lang.Exception
Julia
In Julia, function signatures are written in the following format: commission::Float64
The types in the arguments are used for the multiple dispatch. The return type is validated when the function returns a value, and a runtime exception is raised if the type of the value does not agree with the specified type. Abstract types are allowed and is encouraged for implementing general behavior that is common to all subtypes. The above function can therefore be rewritten as follows. In this case, the function can accept any Integer and Real subtypes accordingly. commission::Real
Types are completely optional in function arguments. When unspecified, it is equivalent to using the type Any, which is the super-type of all types. Finally, it is idiomatic to specify argument types but not return type.
defines a method that returns a general object and takes one integer argument. Objective-C only requires a type in a signature to be explicit when the type is not ; this signature is equivalent: - initWithInt:value;