What is the signature of a method?

Asked

Viewed 24,523 times

48

A signature of the common method/function or constructor is composed of its name + number of parameters + type of parameters?

  • I decided to separate the questions that dealt with two different subjects when the user Leonardo made a another question on the subject. I asked him to separate but he did not return to the site. So here is the separate part.

  • Agree that the tags [tag:independent-of-language] and [tag:terminology]?

3 answers

37


The signature is the way to identify a method uniquely. In languages where several methods may have the same name, you need to have another way to avoid ambiguity. The compiler needs to know which of the methods with the same name you are calling. Then you need to use extra information available in the method to make a decision. The most common is to analyze the parameters. If all parameters are equal, you have the same method, if only one of these parameters is different, you have a different method. It is possible that feedback and other information can be analyzed as well, but this is not common as it may bring some problems. Equally understand that order is also important.

Java is one of the languages that went the safe way of parsing only the parameters. This is common. C# was also this way. C++ chose to use the return type also in some situations.

In theory it would be possible to use any available information, up to the parameter name. In general this would be a bad idea, but nothing prevents a language to use. It is she who will determine what is good for her or not. Java and C# probably used the problems encountered in using C++ to avoid such flexibility.

Let’s look at examples in Java (almost all work well in C# as well):

int FazAlgumaCoisa() { // faz alguma coisa aqui }

int FazAlgumacoisa(int valor) { // faz alguma coisa aqui }

These methods have the same name but are different, they probably perform something (at least) slightly different (although this is not mandatory, it would just be weird to do exactly the same), but not very different, after all, if this occurred, wouldn’t justify the same name. There is a reasonable chance the first just call the second by passing a parameter default (but it doesn’t have to be so). Ex.:

int FazAlgumaCoisa() { FazAlgumaCoisa( 0 ); }

And now look at these methods:

int FazAlgumaCoisa(int valor1) { // faz alguma coisa aqui }

int FazAlgumaCoisa(int valor2) { // faz alguma coisa aqui }

They have the same signature or not?

Possess! You would have a conflict if you declared both. The analysis only considers the type of the parameters, not their name.

But I just put the methods statement, I did not show the class statement. I considered that these methods are of the same class. This means that you may have methods that appear to have the same signature, but in the background the signature is different. See:

int FazAlgumaCoisa() { // faz alguma coisa aqui }

int FazAlgumaCoisa() { // faz alguma coisa aqui }

Is this possible? Well, is it if the methods belong to different classes:

class Exemplo1 {
    int FazAlgumaCoisa() { // faz alguma coisa aqui }
}
class Exemplo2 {
    int FazAlgumaCoisa() { // faz alguma coisa aqui }
}

This way it is valid. What many people don’t understand is that instance methods have an implicit (hidden) parameter. We can call it this. In the background, below the screens, these classes are mounted like this:

class Exemplo1 {
    int FazAlgumaCoisa(Exemplo1 this) { // faz alguma coisa aqui }
}
class Exemplo2 {
    int FazAlgumaCoisa(Exemplo2 this) { // faz alguma coisa aqui }
}

realized that the signature is different? If we use the method that had a int as explicit parameter would look like this:

class Exemplo1 {
    int FazAlgumaCoisa(Exemplo1 this, int valor) { // faz alguma coisa aqui }
}
class Exemplo2 {
    int FazAlgumaCoisa(Exemplo2 this, int valor) { // faz alguma coisa aqui }
}

Some people will talk about the number of method parameters. But it is obvious that if the number of parameters is different the types are different, after all it takes into account all types, then at least you would be comparing a particular type with nothing, which is clearly something different. It is obvious that xxx(int, int) is different from xxx(int, int, int).

Remembering that string, int is different from int, string. Order has relevance.

Other elements can make a difference in the signature depending on the language. See C# in the dcastro response.

See the specification of the Java language (the original question spoke in Java).

The most current C# specification can be found for download or direct access (older version). In his reply dcastro cited the most relevant parts of it.

The specification of C++ must be purchased. A draft is available free of charge.

  • If it is a language like Lua or some other weak typing, how should I consider the signature of a method/function?

  • 1

    Lua doesn’t have methods, but if you think of functions then the signature is usually just the same name. Two functions with the same name in the same scope cannot be disambiguated using other criteria.

13

The answer varies according to the language. In the case of C#, the signature of a method is composed of:

  • the name of the method
  • the number of generic parameters (ex: T)
  • the number of parameters
  • the parameter modifiers (ex: out, ref)
  • the types of the parameters.

The type of return, Generic constraints (ex: where T: IDisposable) and parameter names are not part of the signature.

In relation to overloading:

  • the guys object and dynamic are considered equal;
  • the modifiers this and params are ignored;
  • signatures of two methods belonging to the same type may not differ only by modifiers ref and out. That is, the following 2 methods cannot coexist in the same type:

    void Method(out i);
    void Method(ref i);
    

    But they can be declared in different types in the same hierarchical tree (Hiding method and Overriding).

Section 1.6.6 of the specification:

The Signature of a method must be Unique in the class in which the method is declared. The Signature of a method consists of the name of the method, the number of type Parameters and the number, modifiers, and types of its Parameters. The Signature of a method does not include the Return type.

From section 3.6:

Although out and ref Parameter modifiers are considered part of a Signature, Members declared in a single type cannot differ in Signature solely by ref and out

For the purposes of singatures, the types object and dynamic are considered the same. Members declared in a single type can therefore not differ in Signature solely by object and dynamic.

However, while other elements are not part of the signature of a method (e.g., parameter name), they all contribute to the public API and change them is considered Breaking change.

3

The signature of a method is given by the number and types of arguments of the method, as well as its return value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.