www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - nested function overloading

reply Chris Katko <ckatko gmail.com> writes:
I don't need this functionality, but I wanted to be sure.

Does function overloading not work with nested functions? I got a 
compiler error (something like "function already defined") when I 
tried it.
Jun 17 2022
next sibling parent reply bauss <jj_1337 live.dk> writes:
On Friday, 17 June 2022 at 12:09:33 UTC, Chris Katko wrote:
 I don't need this functionality, but I wanted to be sure.

 Does function overloading not work with nested functions? I got 
 a compiler error (something like "function already defined") 
 when I tried it.
According to the spec then nested functions cannot be overloaded: "Nested functions cannot be overloaded." See: 19.17.1.3 https://dlang.org/spec/function.html#nested
Jun 17 2022
parent reply Chris Katko <ckatko gmail.com> writes:
On Friday, 17 June 2022 at 12:19:33 UTC, bauss wrote:
 On Friday, 17 June 2022 at 12:09:33 UTC, Chris Katko wrote:
 I don't need this functionality, but I wanted to be sure.

 Does function overloading not work with nested functions? I 
 got a compiler error (something like "function already 
 defined") when I tried it.
According to the spec then nested functions cannot be overloaded: "Nested functions cannot be overloaded." See: 19.17.1.3 https://dlang.org/spec/function.html#nested
Thanks! re: documentation. That is one-line tiny footnote in a page that's over 85 pages long on my browser. :) I could easily see many people missing it until they encounter it.
Jun 17 2022
parent bauss <jj_1337 live.dk> writes:
On Friday, 17 June 2022 at 13:04:47 UTC, Chris Katko wrote:
 On Friday, 17 June 2022 at 12:19:33 UTC, bauss wrote:
 On Friday, 17 June 2022 at 12:09:33 UTC, Chris Katko wrote:
 I don't need this functionality, but I wanted to be sure.

 Does function overloading not work with nested functions? I 
 got a compiler error (something like "function already 
 defined") when I tried it.
According to the spec then nested functions cannot be overloaded: "Nested functions cannot be overloaded." See: 19.17.1.3 https://dlang.org/spec/function.html#nested
Thanks! re: documentation. That is one-line tiny footnote in a page that's over 85 pages long on my browser. :) I could easily see many people missing it until they encounter it.
I agree, I didn't know about it either and only found out because I decided to search on the page for functions under nested functions.
Jun 17 2022
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/17/22 8:09 AM, Chris Katko wrote:
 I don't need this functionality, but I wanted to be sure.
 
 Does function overloading not work with nested functions? I got a 
 compiler error (something like "function already defined") when I tried it.
Correct, it's not allowed. However, you can define a function template to get effectively overloading based on parameter type. And you can also use an inner struct to define overloaded functions. -Steve
Jun 20 2022
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Monday, 20 June 2022 at 13:20:51 UTC, Steven Schveighoffer 
wrote:
 And you can also use an inner struct to define overloaded 
 functions.
I believe templates make a better bandaid ```d void main(){ template bar(){ void bar_(int){} void bar_(float){} alias bar=bar_; } bar(1); bar(3.14); } ```
Jun 21 2022
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/22/22 2:05 AM, monkyyy wrote:
 On Monday, 20 June 2022 at 13:20:51 UTC, Steven Schveighoffer wrote:
 And you can also use an inner struct to define overloaded functions.
I believe templates make a better bandaid ```d void main(){     template bar(){         void bar_(int){}         void bar_(float){}         alias bar=bar_;     }     bar(1);     bar(3.14); } ```
Wow, I never thought of that, it's a great idea! You don't even need to use the alias. ```d void main(){ template bar(){ void bar(int){} void bar(float){} } bar(1); bar(3.14); } ``` -Steve
Jun 22 2022
parent Chris Katko <ckatko gmail.com> writes:
On Wednesday, 22 June 2022 at 12:42:48 UTC, Steven Schveighoffer 
wrote:
 On 6/22/22 2:05 AM, monkyyy wrote:
 On Monday, 20 June 2022 at 13:20:51 UTC, Steven Schveighoffer 
 wrote:
 And you can also use an inner struct to define overloaded 
 functions.
I believe templates make a better bandaid ```d void main(){     template bar(){         void bar_(int){}         void bar_(float){}         alias bar=bar_;     }     bar(1);     bar(3.14); } ```
Wow, I never thought of that, it's a great idea! You don't even need to use the alias. ```d void main(){ template bar(){ void bar(int){} void bar(float){} } bar(1); bar(3.14); } ``` -Steve
Wow! That's great! Another trick to add to my toolbox.
Jun 23 2022