Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Type signature
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Defines the inputs and outputs for a function, subroutine or method}} {{About||data to identify the type of a file|File signature|types in universal algebra|Signature (logic)}} {{Multiple issues| {{Cite check|date=September 2021}} {{More citations needed|date=September 2021}} }} In [[computer science]], a '''type signature''' or '''type annotation''' defines the inputs and outputs of a [[Function (computer programming)|function]], [[subroutine]] or [[Method (computer programming)|method]].{{citation needed|date=December 2024}} A type signature includes the number, [[Data type|type]]s, and order of the function's [[Parameter (computer programming)|arguments]]. One important use of a type signature is for function [[Type polymorphism|overload]] resolution, where one particular definition of a function to be called is selected among many overloaded forms. ==Examples== ===C/C++=== In [[C (programming language)|C]] and [[C++]], the type signature is [[Declaration (computer programming)|declared]] by what is commonly known as a [[function prototype]]. In C/C++, a function [[declaration reflects use|declaration reflects its use]]; for example, a [[function pointer]] with the signature {{code|lang=C|(int)(char, double)}} would be called as: <syntaxhighlight lang=C> char c; double d; int retVal = (*fPtr)(c, d); </syntaxhighlight> ===Erlang=== In [[Erlang (programming language)|Erlang]], type signatures may be optionally declared, as:<ref name="erlang.org">{{Cite web |url=https://www.erlang.org/doc/reference_manual/typespec.html#specifications-for-functions |title=Erlang Reference Manual User's Guide Version 13.1.4 |access-date=2023-04-27 |website=erlang.org |archive-url=https://web.archive.org/web/20230127081737/https://www.erlang.org/doc/reference_manual/typespec.html |archive-date=2023-01-27 |url-status=live |at=7.5 Specifications for Functions |language=en}}</ref> <syntaxhighlight lang=Erlang> -spec function_name(type1(), type2(), ...) -> out_type(). </syntaxhighlight> For example: <syntaxhighlight lang=Erlang> -spec is_even(number()) -> boolean(). </syntaxhighlight> ===Haskell=== A type signature in [[Haskell (programming language)|Haskell]] generally takes the following form: <syntaxhighlight lang=Haskell> functionName :: arg1Type -> arg2Type -> ... -> argNType </syntaxhighlight> 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 function]]s; 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 {{Haskell|f x}}, where {{Haskell|f :: a -> b -> c}}, yields a new function {{Haskell|f2 :: b -> c}} that can be called {{Haskell|f2 b}} to produce {{Haskell|c}}. The actual type specifications can consist of an actual type, such as {{Haskell|Integer}}, or a general [[type variable]] that is used in [[type polymorphism|parametric polymorphic]] [[function (programming)|functions]], such as {{Haskell|a}}, or {{Haskell|b}}, or {{Haskell|anyType}}. So we can write something like: {{Haskell|functionName :: a -> a -> ... -> a}} Since Haskell supports [[higher-order function]]s, functions can be passed as arguments. This is written as: {{Haskell|functionName :: (a -> a) -> a}} This function takes in a function with type signature {{Haskell|a -> a}} and returns data of type {{Haskell|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 {{Java|String String.substring(int, int)}} is represented in [[Java bytecode|bytecode]] as {{Java|Ljava/lang/String.substring(II)Ljava/lang/String;}}. The signature of the {{code|main}} method looks like this:<ref>{{Cite web |date=2023-06-08 |title=Signature (functions) - MDN Web Docs Glossary: Definitions of Web-related terms {{!}} MDN |url=https://developer.mozilla.org/en-US/docs/Glossary/Signature/Function |access-date=2024-07-05 |website=developer.mozilla.org |language=en-US}}</ref> <syntaxhighlight lang="java"> public static void main(String[] args); </syntaxhighlight> And in the disassembled bytecode, it takes the form of {{Java|Lsome/package/Main/main:([Ljava/lang/String;)V}} The method signature for the {{code|main()}} method contains three modifiers: * {{Java|public}} indicates that the {{Java|main()}} method can be called by any object. * {{Java|static}} indicates that the {{Java|main()}} method is a class method. * {{Java|void}} indicates that the {{Java|main()}} method has no return value. ==Signature== {{tone|section|date=October 2013}} A function signature consists of the function prototype. It specifies the general information about a function like the name, scope and parameters. Many [[Programming language|programming languages]] use [[name mangling]] in order to pass along more semantic information from the [[compiler]]s 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.<ref>{{cite web|title=C++ Reference: Programming terms|url=http://www.cs.unm.edu/~storm/C++/ProgrammingTerms/FunctionSignatures.html|access-date=3 December 2013}}</ref> Understanding the notion of a function signature is an important concept for all computer science studies. * Modern object orientation techniques make use of [[Java interface|interfaces]], which are essentially templates made from function signatures. * [[C++]] uses [[function overloading]] with various 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 object-oriented programming|polymorphism]] in particular, make much use of the concept of function signature. In the [[C (programming language)|C programming language]], a signature is roughly equivalent to its [[Function prototype|prototype definition]]. In the [[ML (programming language)|ML]] family of [[programming language]]s, "signature" is used as a keyword referring to a construct of the module system that plays the role of an [[interface (computer science)|interface]]. ==Method signature== {{See also|Function prototype}} In [[computer programming]], especially [[object-oriented programming]], a [[Method (computer programming)|method]] is commonly identified by its unique '''method signature''', which usually includes the method name and the number, types, and order of its [[Parameter (computer programming)|parameters]].<ref>{{cite web | access-date = 2011-05-31 | author = Paul Leahy | website= About.com Guide | title = Method Signature | quote = A method signature is part of the method declaration. It is the combination of the method name and the parameter list. | url = http://java.about.com/od/m/g/methodsignature.htm}}</ref> A method signature is the smallest [[datatype|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: <syntaxhighlight lang="c"> int printf(const char*, ... ); </syntaxhighlight> Manipulation of these parameters can be done by using the routines in the standard library header {{C-lang|<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: <!-- The missing comma is deliberate --> <syntaxhighlight lang="cpp"> auto printf(const char*... ) -> int; </syntaxhighlight> ====C#==== Similar to the syntax of C, method signatures in [[C Sharp (programming language)|C#]] are composed of a name and the number and type of its parameters, where the last parameter may be an array of values:<ref> {{cite web | access-date = 2011-08-03 | date = 2002-03-25 | first = Hanspeter | last = Mössenböck | page = 52 | publisher = Institut für Systemsoftware, Johannes Kepler Universität Linz, Fachbereich Informatik | title = Advanced C#: Variable Number of Parameters | url = http://ssw.jku.at/Teaching/Lectures/CSharp/Tutorial/Part1.pdf}} </ref> <syntaxhighlight lang="csharp"> void Add(out int sum, params int[] value); [...] Add(out sum, 3, 5, 7, 11, -1); // sum == 25 </syntaxhighlight> ====Java==== In [[Java (programming language)|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.<ref>{{Cite web|title=Chapter 4. The class File Format|url=https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3.4|access-date=2021-10-17|website=docs.oracle.com}}</ref> For example, the following two methods have different signatures: <syntaxhighlight lang="java"> void doSomething(String[] x); // doSomething(String[]) void doSomething(String x); // doSomething(String) </syntaxhighlight> The following two methods both have the same signature: <syntaxhighlight lang="java"> int doSomething(int x); // doSomething(int) void doSomething(int y) throws Exception; // doSomething(int) </syntaxhighlight> ====Julia==== In [[Julia (programming language)|Julia]], function signatures take the following form: <syntaxhighlight lang="julia"> commission(sale::Int, rate::Float64)::Float64 </syntaxhighlight> 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. <syntaxhighlight lang="julia"> commission(sale::Integer, rate::Real)::Real </syntaxhighlight> 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 [[Include directive|header file]]. For example, <syntaxhighlight lang="objc"> - (id)initWithInt:(int)value; </syntaxhighlight> defines a method {{ObjC|initWithInt}} that returns a general object (an {{ObjC|id}}) and takes one integer argument. Objective-C only requires a type in a signature to be explicit when the type is not {{ObjC|id}}; this signature is equivalent: <syntaxhighlight lang="objc"> - initWithInt:(int)value; </syntaxhighlight> ====Rust==== In [[Rust (programming language)|Rust]], function signatures take the following form: <syntaxhighlight lang="rust"> fn commission(sale: u32, rate: f64) -> f64; </syntaxhighlight> == See also == * {{annotated link|Argument of a function}} ==References== {{reflist}} {{DEFAULTSORT:Type Signature}} <!--Categories--> [[Category:Type theory]] [[Category:Subroutines]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:About
(
edit
)
Template:Ambox
(
edit
)
Template:Annotated link
(
edit
)
Template:C-lang
(
edit
)
Template:Citation needed
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:DMCA
(
edit
)
Template:Haskell
(
edit
)
Template:Java
(
edit
)
Template:Multiple issues
(
edit
)
Template:ObjC
(
edit
)
Template:Reflist
(
edit
)
Template:See also
(
edit
)
Template:Short description
(
edit
)
Template:Tone
(
edit
)