Type signature

Last updated

In computer science, a type signature or type annotation defines the inputs and outputs of a function, subroutine or method. A type signature includes the number, types, and order of the function's arguments. One important use of a type signature is for function overload resolution, where one particular definition of a function to be called is selected among many overloaded forms.

Contents

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:

charc;doubled;intretVal=(*fPtr)(c,d);

Erlang

In Erlang, type signatures may be optionally declared, as: [1]

-specfunction_name(type1(),type2(),...)->out_type().

For example:

-specis_even(number())->boolean().

Haskell

A type signature in Haskell generally takes the following form:

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 is supplied and the function is "curried" to produce a function for the argument not supplied. Thus, calling fx, where f::a->b->c, yields a new function f2::b->c that can be called f2b 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 StringString.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:

publicstaticvoidmain(String[]args);

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:

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 (stored internally to most compilers) which is not readily available, but may be accessed. [2]

Understanding the notion of a function signature is an important concept for all computer science studies.

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, a signature is roughly equivalent to its prototype definition.

The term "signature" may carry other meanings in computer science:

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. [3] 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:

intprintf(constchar*,...);

Manipulation of these parameters can be done by using the routines in the standard library header <stdarg.h>.

In C++, the return type can also follow the parameter list, which is referred to as a trailing return type. The difference is only syntactic; in either case, the resulting signature is identical:

autoprintf(constchar*...)->int;

C#

Similar to the syntax of C, method signatures in C# are composed of a name and the number and type of its parameters, where the last parameter may be an array of values: [4]

voidAdd(outintsum,paramsint[]value);[...]Add(outsum,3,5,7,11,-1);// sum == 25

Java

In Java, a method signature is composed of a 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, nor are the names of parameters; they are ignored by the compiler for checking method uniqueness.

The method signatures help distinguish overloaded methods (methods with the same name) in a class. Return types are not included in overloading. Only method signatures should be used to distinguish overloaded methods. [5]

For example, the following two methods have different signatures:

voiddoSomething(String[]x);// doSomething(String[])voiddoSomething(Stringx);// doSomething(String)

The following two methods both have the same signature:

intdoSomething(intx);// doSomething(int)voiddoSomething(inty)throwsException;// doSomething(int)

Julia

In Julia, function signatures take the following form:

commission(sale::Int,rate::Float64)::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 are 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(sale::Integer,rate::Real)::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. It is idiomatic to specify argument types but not return type.

Objective-C

In the Objective-C programming language, method signatures for an object are declared in the interface header file. For example,

-(id)initWithInt:(int)value;

defines a method initWithInt that returns a general object (an id) and takes one integer argument. Objective-C only requires a type in a signature to be explicit when the type is not id; this signature is equivalent:

-initWithInt:(int)value;

Rust

In Rust, function signatures take the following form:

fncommission(sale: u32,rate: f64)-> f64;

See also

Related Research Articles

Generic programming is a style of computer programming in which algorithms are written in terms of data types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by the ML programming language in 1973, permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplicate code.

Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of its arguments. This is a generalization of single-dispatch polymorphism where a function or method call is dynamically dispatched based on the derived type of the object on which the method has been called. Multiple dispatch routes the dynamic dispatch to the implementing function or method using the combined characteristics of one or more arguments.

In object-oriented (OO) and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:

In computer programming, a parameter or a formal argument is a special kind of variable used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are the values of the arguments with which the subroutine is going to be called/invoked. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call are evaluated, and the resulting values can be assigned to the corresponding parameters.

<span class="mw-page-title-main">C syntax</span> Set of rules defining correctly structured programs

The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. In some languages, particularly C++, function objects are often called functors.

In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.

Partial template specialization is a particular form of class template specialization. Usually used in reference to the C++ programming language, it allows the programmer to specialize only some arguments of a class template, as opposed to explicit full specialization, where all the template arguments are provided.

In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In computer programming, a function prototype or function interface is a declaration of a function that specifies the function's name and type signature, but omits the function body. While a function definition specifies how the function does what it does, a function prototype merely specifies its interface, i.e. what data types go in and come out of it. The term "function prototype" is particularly used in the context of the programming languages C and C++ where placing forward declarations of functions in header files allows for splitting a program into translation units, i.e. into parts that a compiler can separately translate into object files, to be combined by a linker into an executable or a library. The function declaration precedes the function definition, giving details of name, return type, and storage class along with other relevant attributes.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages.

In some programming languages, const is a type qualifier that indicates that the data is read-only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in that it is part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time on each use. Languages which use it include C, C++, D, JavaScript, Julia, and Rust.

A class in C++ is a user-defined type or data structure declared with any of the keywords class, struct or union that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class declared with the keyword class is private. The private members are not accessible outside the class; they can be accessed only through member functions of the class. The public members form an interface to the class and are accessible outside the class.

C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

References

  1. "Erlang Reference Manual User's Guide Version 13.1.4". erlang.org. 7.5 Specifications for Functions. Archived from the original on 2023-01-27. Retrieved 2023-04-27.
  2. "C++ Reference: Programming terms" . Retrieved 3 December 2013.
  3. Paul Leahy. "Method Signature". About.com Guide. Retrieved 2011-05-31. A method signature is part of the method declaration. It is the combination of the method name and the parameter list.
  4. Mössenböck, Hanspeter (2002-03-25). "Advanced C#: Variable Number of Parameters" (PDF). Institut für Systemsoftware, Johannes Kepler Universität Linz, Fachbereich Informatik. p. 52. Retrieved 2011-08-03.
  5. "Chapter 4. The class File Format". docs.oracle.com. Retrieved 2021-10-17.