4

This question originated in a discussion with a colleague and it is purely academic.

Is there any programming language that has variable overloading?

In Java and many other languages, there is function overloading, where multiple functions/methods with the same name can be declared, and the compiler chooses which function to execute, based on the parameters the function was called with.

Is there any programming language (including exotic ones) that uses variable overloading, where multiple variables with the same name but different types can be created and the compiler chooses the variable based on the required type?

E.g.

int x = 1;
String x = "test";
print(x); // prints "test" because the print function requires a string.

I can't think of a reason why you would want this, so the question is purely academic.

Dakkaron
  • 5,930
  • 2
  • 36
  • 51
  • 1
    C# lets you do this, I think? If you have a class with different typed fields, you can define implicit [conversion operators](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators) on the class that auto-convert it when used as that type. Is that what you were looking for? – General Grievance Apr 21 '21 at 16:01

0 Answers0