Jump to page: 1 2
Thread overview
Inner function overload bug?
Jan 08, 2013
Era Scarecrow
Jan 08, 2013
Philippe Sigaud
Jan 08, 2013
Era Scarecrow
Jan 08, 2013
Philippe Sigaud
Jan 09, 2013
bearophile
Jan 09, 2013
Andrej Mitrovic
Jan 09, 2013
Era Scarecrow
Jan 09, 2013
Philippe Sigaud
Jan 09, 2013
Era Scarecrow
Jan 09, 2013
Philippe Sigaud
Jan 09, 2013
dennis luehring
Jan 09, 2013
dennis luehring
Jan 09, 2013
Era Scarecrow
Jan 11, 2013
Timon Gehr
Jan 11, 2013
Era Scarecrow
Jan 11, 2013
Brad Roberts
January 08, 2013
 Hmmm...  Curious case, I think it's a bug; Depends on if inner functions qualify for overloading or not.

  //globally or in struct/class/union works fine
  int test() {return 0;}
  void test(int x) {}

  unittest {
    int test() {return 0;}
    void test(int x) {} //test already defined
  }

  void func() {
    int test() {return 0;}
    void test(int x) {} //test already defined

    static int test2() {return 0;}
    static void test2(int x) {}  //test2 already defined
  }
January 08, 2013
It's conform to the spec

http://dlang.org/function.html

Last line of the 'nested functions' subsection:

"Nested functions cannot be overloaded."
Nested functions cannot be overloaded.


January 08, 2013
On Tuesday, 8 January 2013 at 21:12:34 UTC, Philippe Sigaud wrote:
> It's conform to the spec
>
> http://dlang.org/function.html
>
> Last line of the 'nested functions' subsection:
>
> "Nested functions cannot be overloaded."
> Nested functions cannot be overloaded.

 Hmm I seemed to have missed that.

 That just means my unittest wouldn't work, so either it has to be global or inside a struct/union/class (and global is not very likely).

 Thanks for clearing that up.
January 08, 2013
On Tue, Jan 8, 2013 at 10:43 PM, Era Scarecrow <rtcvb32@yahoo.com> wrote:
>  Hmm I seemed to have missed that.
>
>  That just means my unittest wouldn't work, so either it has to be global or
> inside a struct/union/class (and global is not very likely).
>
>  Thanks for clearing that up.

I had a bunch of troubles trying to define nested functions automatically generated. The fact that they cannot be recursive killed the deal for me (that was for a parser, which needs recursion).
January 09, 2013
Philippe Sigaud:

> I had a bunch of troubles trying to define nested functions
> automatically generated. The fact that they cannot be recursive killed
> the deal for me (that was for a parser, which needs recursion).

If you define an inner static struct, its static methods can call each other freely.

Bye,
bearophile
January 09, 2013
On 1/9/13, bearophile <bearophileHUGS@lycos.com> wrote:
> If you define an inner static struct, its static methods can call each other freely.

You can also use a mixin template:

mixin template S()
{
    void test(ref int x) { x = test(); }
    int test() { return 1; }
}

void main()
{
    mixin S;

    int x;
    test(x);
    assert(x == 1);
}
January 09, 2013
On Wednesday, 9 January 2013 at 00:53:16 UTC, Andrej Mitrovic wrote:
> mixin template S()
> {
>     void test(ref int x) { x = test(); }
>     int test() { return 1; }
> }
>
> void main()
> {
>     mixin S;
>
>     int x;
>     test(x);
>     assert(x == 1);
> }

 An interesting idea, but the more obvious uses it would be for is bitfields or something similar, which is generated text and mixed in. Would you do a mixin within a mixin template to get that to work? I'd rather just mixin directly to the function.

//works with above main?
mixin template S()
{
    mixin(bitfields!(
            int, "test", 8,
    ));
}
January 09, 2013
On Wed, Jan 9, 2013 at 1:53 AM, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> On 1/9/13, bearophile <bearophileHUGS@lycos.com> wrote:
>> If you define an inner static struct, its static methods can call each other freely.
>
> You can also use a mixin template:
>
> mixin template S()
> {
>     void test(ref int x) { x = test(); }
>     int test() { return 1; }
> }
>
> void main()
> {
>     mixin S;
>
>     int x;
>     test(x);
>     assert(x == 1);
> }

That's weird. Why does that work? Directly pasting the mixin content in main() does not compile, right?
January 09, 2013
On Wednesday, 9 January 2013 at 10:53:53 UTC, Philippe Sigaud wrote:
> On Wed, Jan 9, 2013 at 1:53 AM, Andrej Mitrovic wrote:
>> On 1/9/13, bearophile wrote:
>>> If you define an inner static struct, its static methods can call each other freely.
>>
>> You can also use a mixin template:
>>
>> mixin template S()
>> {
>>     void test(ref int x) { x = test(); }
>>     int test() { return 1; }
>> }
>>
>> void main()
>> {
>>     mixin S;
>>
>>     int x;
>>     test(x);
>>     assert(x == 1);
>> }
>
> That's weird. Why does that work? Directly pasting the mixin content in main() does not compile, right?

 I can only assume if it does work, that the mixin template has it's own scope that enables the overloading. If you can't do it with one, you shouldn't be allowed to do it with the other.

 Overloading within nested functions is likely a rare use case.

January 09, 2013
Am 08.01.2013 22:43, schrieb Era Scarecrow:
> On Tuesday, 8 January 2013 at 21:12:34 UTC, Philippe Sigaud wrote:
>> It's conform to the spec
>>
>> http://dlang.org/function.html
>>
>> Last line of the 'nested functions' subsection:
>>
>> "Nested functions cannot be overloaded."
>> Nested functions cannot be overloaded.
>
>    Hmm I seemed to have missed that.
>
>    That just means my unittest wouldn't work, so either it has to
> be global or inside a struct/union/class (and global is not very
> likely).
>
>    Thanks for clearing that up.
>

isn't that some sort of hijacking then?
« First   ‹ Prev
1 2