www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get Function Body

reply bauss <jj_1337 live.dk> writes:
Is there a way to retrieve the body of a function as a string?

Scenario.

I want to pass a function to a mixin template and just mixin the 
body of the function.

Ex.

mixin template Foo(alias fun) {
     void bar() {
         mixin(getBodyOfFun(fun));
     }
}

I'm aware that I can pass a string like mixin Foo!"a > b" but I 
would really like to avoid that.

I also can't just call "fun" as normal, unless it can be forced 
to be inline within the mixin template. The reason is if I mixin 
two mixin templates with the same function passed it must exist 
as two different function bodies executed, even tho they do the 
same.

Which means the following must have two "different" baz and not 
the actual baz.

void baz() { ... }

mixin Foo!baz;
mixin Foo!baz;

I don't know if it'll be possible without some sort of "parsing" 
the function or even without "string function bodies".
Jun 30 2017
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Friday, 30 June 2017 at 16:38:45 UTC, bauss wrote:
 Is there a way to retrieve the body of a function as a string?

 Scenario.

 I want to pass a function to a mixin template and just mixin 
 the body of the function.

 Ex.

 mixin template Foo(alias fun) {
     void bar() {
         mixin(getBodyOfFun(fun));
     }
 }

 I'm aware that I can pass a string like mixin Foo!"a > b" but I 
 would really like to avoid that.

 I also can't just call "fun" as normal, unless it can be forced 
 to be inline within the mixin template. The reason is if I 
 mixin two mixin templates with the same function passed it must 
 exist as two different function bodies executed, even tho they 
 do the same.

 Which means the following must have two "different" baz and not 
 the actual baz.

 void baz() { ... }

 mixin Foo!baz;
 mixin Foo!baz;

 I don't know if it'll be possible without some sort of 
 "parsing" the function or even without "string function bodies".
You need to function body as a string. There is no way of retriving a functionBodyString from the compiler. And I suspect it would be a bad idea to be able to do so. Since the compiler may mutate the body while processing/optimizing.
Jun 30 2017
parent bauss <jj_1337 live.dk> writes:
On Friday, 30 June 2017 at 16:43:33 UTC, Stefan Koch wrote:
 On Friday, 30 June 2017 at 16:38:45 UTC, bauss wrote:
 Is there a way to retrieve the body of a function as a string?

 Scenario.

 I want to pass a function to a mixin template and just mixin 
 the body of the function.

 Ex.

 mixin template Foo(alias fun) {
     void bar() {
         mixin(getBodyOfFun(fun));
     }
 }

 I'm aware that I can pass a string like mixin Foo!"a > b" but 
 I would really like to avoid that.

 I also can't just call "fun" as normal, unless it can be 
 forced to be inline within the mixin template. The reason is 
 if I mixin two mixin templates with the same function passed 
 it must exist as two different function bodies executed, even 
 tho they do the same.

 Which means the following must have two "different" baz and 
 not the actual baz.

 void baz() { ... }

 mixin Foo!baz;
 mixin Foo!baz;

 I don't know if it'll be possible without some sort of 
 "parsing" the function or even without "string function 
 bodies".
You need to function body as a string. There is no way of retriving a functionBodyString from the compiler. And I suspect it would be a bad idea to be able to do so. Since the compiler may mutate the body while processing/optimizing.
Well in my case I don't want optimization or anything like that, in fact the original function wouldn't matter at all. I guess I'll go with the string way then though. I'm aware this is probably not a scenario day-to-day code will need, but in my case I need to make my program as obscure as possible.
Jun 30 2017