www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - immuable method address and export method address.

reply "Igor Stepanov" <wazar.leollone yahoo.com> writes:
Hello, I've two simple questions:

1. I have a structure:

struct Foo
{
   public int bar() const {return 0;}
   public int bar() immutable {return 1;}
}

How can I get immutable bar address?

When I've wrote next code I've got a const bar address. Is it a 
bug?

void main()
{
     immutable Foo boo;
     int delegate() immutable dg = &boo.bar;
}

Next question:
If I write туче code, it'll be builded successfully

void main()
{
     foreach(cur; __traits(getOverloads, Foo, "bar"))
     {
         void* p = &cur;
         writeln(cur); //prints a function pointer.
     }
}

If I change protection of bar to export I've got a error:

struct Foo
{
   export int bar() const {return 0;}
   export int bar() immutable {return 1;}
}

void main()
{
     foreach(cur; __traits(getOverloads, Foo, "bar"))
     {
         void* p = &cur; //Error: need 'this' to access member bar
         writeln(cur);
     }
}

How should it be?
May 05 2013
parent "deadalnix" <deadalnix gmail.com> writes:
On Sunday, 5 May 2013 at 19:34:34 UTC, Igor Stepanov wrote:
 Hello, I've two simple questions:

 1. I have a structure:

 struct Foo
 {
   public int bar() const {return 0;}
   public int bar() immutable {return 1;}
 }

 How can I get immutable bar address?

 When I've wrote next code I've got a const bar address. Is it a 
 bug?

 void main()
 {
     immutable Foo boo;
     int delegate() immutable dg = &boo.bar;
 }
Yes. First, the immutable keyword before dg don't do what you think in D. Right now this is an hole in the type system/spec/implementation (pick one). But boo being immutable it should return the immutable delegate anyway. So you have two bugs in one. Congrat ! Hopefully, I've been able to discuss this with Andrei at DConf, who agreed on the problem and we were able to discuss solutions.
 Next question:
 If I write туче code, it'll be builded successfully

 void main()
 {
     foreach(cur; __traits(getOverloads, Foo, "bar"))
     {
         void* p = &cur;
         writeln(cur); //prints a function pointer.
     }
 }

 If I change protection of bar to export I've got a error:

 struct Foo
 {
   export int bar() const {return 0;}
   export int bar() immutable {return 1;}
 }

 void main()
 {
     foreach(cur; __traits(getOverloads, Foo, "bar"))
     {
         void* p = &cur; //Error: need 'this' to access member 
 bar
         writeln(cur);
     }
 }

 How should it be?
The method aren't static, so you should need this all the time. I can understand both behavior, but it is clearly inconsistent. We should pick one and stick to it.
May 05 2013