Jump to content

Type signature

From Wikipedia, the free encyclopedia

Incomputer science,atype signatureortype annotationdefines the inputs and outputs of afunction,subroutineormethod.A type signature includes the number,types,and order of the function'sarguments.One important use of a type signature is for functionoverloadresolution, where one particular definition of a function to be called is selected among many overloaded forms.

Examples[edit]

C/C++[edit]

InCandC++,the type signature isdeclaredby what is commonly known as afunction prototype.In C/C++, a functiondeclaration reflects its use;for example, afunction pointerwith the signature(int)(char,double)would be called as:

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

Erlang[edit]

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

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

For example:

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

Haskell[edit]

A type signature inHaskellgenerally 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 ofcurrying,which is made possible by Haskell's support forfirst-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, callingfx,wheref::a->b->c,yields a new functionf2::b->cthat can be calledf2bto producec.

The actual type specifications can consist of an actual type, such asInteger,or a generaltype variablethat is used inparametric polymorphicfunctions,such asa,orb,oranyType.So we can write something like: functionName::a->a->...->a

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

This function takes in a function with type signaturea->aand returns data of typeaout.

Java[edit]

In theJava virtual machine,internal type signaturesare used to identify methods and classes at the level of the virtual machine code.

Example: The methodStringString.substring(int,int)is represented inbytecodeasLjava/lang/String.substring(II)Ljava/lang/String;.

The signature of themainmethod looks like this:[2]

publicstaticvoidmain(String[]args);

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

The method signature for themain()method contains three modifiers:

  • publicindicates that themain()method can be called by any object.
  • staticindicates that themain()method is a class method.
  • voidindicates that themain()method has no return value.

Signature[edit]

A function signature consists of the function prototype. It specifies the general information about a function like the name, scope and parameters. Manyprogramming languagesusename manglingin order to pass along more semantic information from thecompilersto 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.[3]

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

  • Modern object orientation techniques make use ofinterfaces,which are essentially templates made from function signatures.
  • C++usesfunction overloadingwith various signatures.

The practice ofmultiple inheritancerequires consideration of the function signatures to avoid unpredictable results. Computer science theory, and the concept ofpolymorphismin particular, make much use of the concept of function signature.

In theC programming language,a signature is roughly equivalent to itsprototype definition.

In theMLfamily ofprogramming languages,"signature" is used as a keyword referring to a construct of the module system that plays the role of aninterface.

Method signature[edit]

Incomputer programming,especiallyobject-oriented programming,amethodis commonly identified by its uniquemethod signature,which usually includes the method name and the number, types, and order of itsparameters.[4]A method signature is the smallesttypeof a method.

Examples[edit]

C/C++[edit]

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 atrailing return type.The difference is only syntactic; in either case, the resulting signature is identical:

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

C#[edit]

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

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

Java[edit]

InJava,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.[6]

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[edit]

InJulia,function signatures take the following form:

commission(sale::Int,rate::Float64)::Float64

The types in the arguments are used for themultiple 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[edit]

In theObjective-Cprogramming language, method signatures for an object are declared in the interface header file. For example,

-(id)initWithInt:(int)value;

defines a methodinitWithIntthat returns a general object (anid) and takes one integer argument. Objective-C only requires a type in a signature to be explicit when the type is notid;this signature is equivalent:

-initWithInt:(int)value;

Rust[edit]

InRust,function signatures take the following form:

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

See also[edit]

References[edit]

  1. ^"Erlang Reference Manual User's Guide Version 13.1.4".erlang.org.7.5 Specifications for Functions.Archivedfrom the original on 2023-01-27.Retrieved2023-04-27.
  2. ^"Signature (functions) - MDN Web Docs Glossary: Definitions of Web-related terms | MDN".developer.mozilla.org.2023-06-08.Retrieved2024-07-05.
  3. ^"C++ Reference: Programming terms".Retrieved3 December2013.
  4. ^Paul Leahy."Method Signature".About.com Guide.Retrieved2011-05-31.A method signature is part of the method declaration. It is the combination of the method name and the parameter list.
  5. ^ 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.Retrieved2011-08-03.
  6. ^"Chapter 4. The class File Format".docs.oracle.com.Retrieved2021-10-17.