I have noticed that JavaScript and PHP and Python do not support function overloading.

Do all dynamically typed languages not support function overloading? If the answer is yes, then why is that?

  • 2
    Sharing your research helps everyone. Tell us what you've tried and why it didn't meet your needs. This demonstrates that you've taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Apr 14 at 8:45
  • 2
    ...also, please don't cross-post: stackoverflow.com/questions/67085246/… "Cross-posting is frowned upon as it leads to fragmented answers splattered all over the network..." – gnat Apr 14 at 8:46
  • 4
    This depends on what, precisely, you mean by "overloading". If you mean "compile time static dispatch based on compile time static types", then no, that is impossible for a language which doesn't have static types for obvious reasons. But there are plenty of dynamically typed languages that support overloading based on the number of arguments, the runtime type of arguments, or even arbitrary dispatch functions. – Jörg W Mittag Apr 14 at 15:04

The issue is easier to understand if you consider carefully what it really means to overload a function.

Overloaded methods e.g. in Java are really two completely separate entities; they share no byte code, no address, nothing except their name; and their name isn't really the same either, since in the compiler symbol table, a print() method for ints and a print() method Strings actually have a mangled name that contains both the user-visible identifier ("print") and additional information encoding the argument type.

Now contrast this with Javascript, where a print() function really is called print and nothing else. The runtime system only knows that it is a function; what arguments it expects and how it deals with them is entirely defined by the code in the function's body. Therefore, defining a second function "print" simply overwrites the previous one rather than add a second implementation.

The details vary a bit from language to language, but the gist is, if you don't have an explicit representation of data types in your compile-time/run-time system, you can't use them to tell elements of the system apart, and that is why overloading on types is largely restricted to systems with a strong presence of types in the language definition.

  • +1 Good answer. One curiosity I found when I used to use Jython is that when you called an overloaded Java method from Python code, it would be resolved with double-dispatch which neither language supported. – JimmyJames Apr 14 at 18:39

Dynamically typed languages may use double dispatch (https://en.m.wikipedia.org/wiki/Double_dispatch) to implement functions that depend on an argument's type.

For example, in Smalltalk there is a method print: anObject defined in class Stream that defers the actual implementation of how an object should be printed to the argument:

print: anObject
    anObject printOn: self

If the implementation depends on the types of both recipient and argument (for example in the numeric classes for mixed arithmetics) the indirect method would be specific to the original receiver's class, so the method + in class Integer could look like this:

+ aNumber
    ^aNumber addToInteger: self

where different numeric classes implement addToInteger: differently.


Additional to other answers, there are dynamically typed languages where you can overload functions with different number of arguments (a.k.a "arity"). For example, there is erlang:

add(X,Y) -> 
   Z = X+Y, 
add(X,Y,Z) -> 
   A = X+Y+Z, 

Method overloading and overriding are both mechanisms where the language select between multiple function/method implementation with the same name. According to the most common definition, overloading is resolved at compile-time depending on the static types and number of arguments, while overriding (also known is dynamic dispatch) is resolved at runtime.

So almost by definition, dynamic languages would not support overloading, since it is static, not dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.