Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading in local methods and lambda

Tags:

c#

lambda

c#-7.0

static void Main() {
  void TestLocal() => TestLocal("arg");
  TestLocal("arg");
}
static void TestLocal(string argument) {
}

In this example, local function has the same name as another method, which I want to overload. But this method is invisible from inside of this function, and even from inside of Main(). The same is for lambdas:

static void Main() {
  Action TestLambda = () => TestLambda("arg");
}
static void TestLambda(string argument) {
}

I understand, why lambda hides outer method - because it is a local variable, and local variables always work this way. But I don't see any reason for local functions hide and not overload outer methods - it could be very useful for reducing amout of arguments in local scope, doing some kind of carrying.

Why local functions hide methods?

EDIT:

I can imagine an example where it gets complicated

void Bar(short arg) {
  Console.WriteLine("short");
}
void Bar(byte arg) {
  Console.WriteLine("byte");
}
void Test() {
  void Bar(int arg) {
    Console.WriteLine("int");
  }
  Bar(0);
}

It was already complicated enough with argument type resolution. If they added overloading to local methods, it would be one more stupid task for job interviews and one more huge task for compiler makers. And there are also virtual and new methods...

like image 761
Alex Butenko Avatar asked Feb 02 '18 09:02

Alex Butenko


People also ask

Can lambda functions be overloaded?

No, you can not overload the lambda! Which is not possible, as the same variable name can not be reused in C++.

Is function overloading good practice?

You need to be careful while overloading a method in Java, especially after the introduction of autoboxing in Java 5. Poorly overloaded method not only adds confusion among developers who use that but also they are error-prone and leaves your program at compiler's mercy to select proper method.


1 Answers

Why local functions hide methods?

Basically it's introducing the method name into the declaration space inside the method. Within that declaration space, the name only refers to the local method... just as it does for a local variable. I think that's reasonably consistent with the way that names introduced into methods have always worked.

I'd personally advise against trying to do this anyway, as it'll cause confusion, but if you really need to refer to the class method, just make it explicit:

ClassName.TestLocal("arg");

What I do think could be fixed is that local methods can't be overloaded between themselves. You can't write:

void Foo()
{
    void Method(int x) {}
    void Method(string y) {}
}

This is for a related reason - a method's declaration space can't include the same name twice, whereas a class's declaration space can do so, in terms of method overloading. Maybe the rules will be loosened around this...

like image 66
Jon Skeet Avatar answered Oct 11 '22 06:10

Jon Skeet