Hello,
Following the medium article
https://medium.com/coinmonks/solidity-and-object-oriented-programming-oop-191f8deb8316
the section on polymorphism states an example that holds 2 exactly identical function names - MyQuestion: how is it possible to have 2 identical function names (getVariableData) - I understood that is not allowed ??
Thanks
Hi @yestome,
Solidity does allow this, if the number of parameters is different or if a parameter’s data type is different.
When a function call is made, and there is more than one function with the same name, the function that is selected for execution is the one whose parameter types, or number of parameters, match the arguments the function is called with.
Here is a link to the relevant section in the Solidity documentation: Function Overloading, and the following subsection Overload resolution and Argument matching. Here you will find further details and some examples:
https://docs.soliditylang.org/en/v0.8.4/contracts.html#function-overloading
Just let me know if you have any further questions about this.
wow - ok - that’s uncommon …
from an intuitive perspective it feels unsafe, couldn’t that lead to possible errors in code processing, i.e. is that distinction following different parameters solid?
But let me explain my point with an example: Let’s assume for a second that this is a person calling the function being asked to enter parameters. He makes a mistake and - taking the example of the solidity documentation - forgets to provide the 2nd “bool” parameter -> result, the first, instead of the second function would be executed creating an error or false execution?
Am I misinterpreting something here or is this a somewhat unsafe way to allow identical function names potentially leading to errors?
And what is the reason/intention for enabling function overloading in the first place?
Curious to learn more about this.
I am not a trained computer scientist, but my understanding is that polymorphism is actually a common feature in statically-typed programming languages such as Solidity e.g. C++, Java, Kotlin, Swift.
If you are only familiar with dynamically-typed programming languages (e.g. JavaScript, Python, PHP) then polymorphism will seem odd until you understand what makes these two groups of languages different.
Reference values (e.g. variables, parameters etc.) in statically-typed languages are constrained in terms of the data type they can represent. Solidity is also explicitly-typed, meaning that data types need to be declared e.g.
// Valid code:
function example(uint256 amount, address recipient, bool express) {...}
// Invalid code:
function example(amount, recipient, express) {...}
This is different to a dynamically-typed language such as JavaScript where the invalid Solidity code above would, instead, be perfectly valid. And without the type declarations, it would potentially be possible to call the function with arguments which pass any value type to each of the parameters. What’s more, in JavaScript, we would be free to call the above function with less arguments than the number of parameters e.g.
example(100);
This function call would pass 100
 as the amount
parameter, with the recipient
and express
parameters being assigned undefined
. However — and this is key — if the Solidity function is called with either (i) less arguments than the number of parameters; or (ii) arguments with value types that do not match the declared value types of their corresponding parameters, then the function will not execute. But it is precisely these constraints, in both the number and the data type of the arguments in Solidity, which allow its functions to be overloaded.
This discussion thread is talking about Java, but I think the reasons given for using function overloading should be broadly applicable to most programming languages that allow it.
https://stackoverflow.com/questions/38537340/why-should-i-ever-overload-methods
As you will have seen from the discussion thread, function overloading is designed to be used for cases where the same basic functionality (or computation) needs to cater for inputs which differ in data type and or number. So, the developer would need to weigh up the advantages of allowing the user flexibility in the data they input into the frontend interface, and the potential negative consequences this flexibility could cause. Many potential input-related consequences can be avoided by implementing robust input validation in the frontend, in order to limit the extent of flexibility allowed in terms of the data input.
I hope that goes some way towards answering your question. I’m tagging my colleague @thecil, as he has experience with C++, and he may be able to add some further insights about this.
Jon,
Great link with revealing insights, gained an understanding of the use of overloading - makes perfect sense now. - thank you very much for this detailed answer!