Thread overview
[Issue 12578] Allow local function overloading
Apr 14, 2014
Jonathan M Davis
Apr 15, 2014
Walter Bright
Apr 15, 2014
Jonathan M Davis
Apr 15, 2014
Kenji Hara
Apr 15, 2014
Philpax
Apr 23, 2014
Kenji Hara
Sep 19, 2019
Simen Kjaeraas
Aug 04, 2020
Simen Kjaeraas
Dec 17, 2022
Iain Buclaw
April 14, 2014
https://issues.dlang.org/show_bug.cgi?id=12578

--- Comment #1 from monarchdodra@gmail.com ---
Also relevant: The declaration of nested structs/classes: https://github.com/D-Programming-Language/phobos/pull/2074/files

--
April 14, 2014
https://issues.dlang.org/show_bug.cgi?id=12578

Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com

--- Comment #2 from Jonathan M Davis <jmdavisProg@gmx.com> ---
It would also be in line with the "turtles all the way down" approach that we tend to favor to have nested functions be overloadable just like non-nested functions can be. The fact that nested functions can't be overloaded is inconsistent with functions in the rest of the language.

--
April 15, 2014
https://issues.dlang.org/show_bug.cgi?id=12578

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> ---
The reasons for this behavior are twofold:

1. Local functions aren't visible outside of their scope. Their use is pretty limited, and so there isn't much of any benefit to overloadability.

2. Forward references aren't allowed in local scope, meaning any use of overloading would be fairly restricted anyway.

It's not impossible to overcome this, it just seems pointless.

--
April 15, 2014
https://issues.dlang.org/show_bug.cgi?id=12578

--- Comment #4 from Jonathan M Davis <jmdavisProg@gmx.com> ---
I frequently need helper functions in unittest blocks, and the lack of ability to overload them can be quite annoying. So, in my experience, the lack of visibility outside of the local scope hasn't really stopped the need for overloading - rather the lack of overloading just ends up forcing you to move the nested functions into an outer scope where they don't belong in order to get the overloading to work. And while there may be cases where forward references might be needed, I don't recall any cases where that's been a problem for me.

--
April 15, 2014
https://issues.dlang.org/show_bug.cgi?id=12578

--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> ---
(In reply to Walter Bright from comment #3)
> 2. Forward references aren't allowed in local scope, meaning any use of overloading would be fairly restricted anyway.

By adding following rule for function local declarations, we can relax the restriction.

- If no runtime statements exist between two local declarations,
  compiler will declare them in one DeclDef scope.

Examples:
----

void main() {
  //{
    void foo()
    void foo(int) {}
    // These are declared in one DeclDef scope, so they can be overloaded each
other.
  //}
}

The rule will work on general case.

void main() {
    struct S {
        int foo() { bar(); }
        static assert(x == 10);
    }
    void bar() { S s; }

    enum x = 10;

    // Declaraing S, bar, and x have no runtime side effect,
    // so they can be referred to each other.
}

If same name function declarations are divided to multiple DeclDefs, they cannot be overloaded.

void main() {
    void foo()
    void foo(int) {}
    // foo == overloaded function
    // --> ok

    int x;  // runtime statement divides DeclDefs

    void foo(string) {}
    // Another declaration of foo will conflict with the overloaded foo.
    // --> error
}

--
April 15, 2014
https://issues.dlang.org/show_bug.cgi?id=12578

Philpax <phillip@philliplarkson.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |phillip@philliplarkson.com

--- Comment #6 from Philpax <phillip@philliplarkson.com> ---
My use-case for this was implementing a toString function for different types in my view template (i.e. embedded D in a display layout) code. Without it, I had to name each variant of the function something unique, which led to function names like toStringSerializeNode(serializeNode) and toStringDeserializeNode(deserializeNode) before I switched to the suggested static struct solution.

As was mentioned, this would also be helpful in unit tests and other areas in which helper functions for multiple types would be convenient. I personally believe it's incongruous to not support overloading in local scope; D treats local functions similarly to global functions in all the cases I've encountered, but this seems like one case where the behaviour doesn't match up.

--
April 23, 2014
https://issues.dlang.org/show_bug.cgi?id=12578

--- Comment #7 from Kenji Hara <k.hara.pg@gmail.com> ---
One more issue which has same root is a mutual call of local functions.

void test() {
  void foo() { bar(); } // error, bar not defined
  void bar() { foo(); } // ok
}

My proposal in comment#5 could resolve the case with same way.

--
September 19, 2019
https://issues.dlang.org/show_bug.cgi?id=12578

Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com

--- Comment #8 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
An example of how this limitation has consequences outside of simple nested functions is available in issue 20226, where selectively imported functions with identical names aren't callable due to this issue.

--
August 04, 2020
https://issues.dlang.org/show_bug.cgi?id=12578

Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |porton@narod.ru

--- Comment #9 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
*** Issue 21107 has been marked as a duplicate of this issue. ***

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=12578

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--