www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Capturing Caller UDAs as CT Template Function Parameters

reply "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> writes:
As a follow up to the most

http://forum.dlang.org/thread/miri9k$2p5$1 digitalmars.com

I'm now very much interested in finding a way to make yield() 
capture the UDAs of its caller. That is instead of

void yield(T)(ref T value)

I want it to get a hold of the UDAs of the calling function and 
somehow propagate them as compile-time template arguments. 
Something like

void yield(T, callerAttributes = __traits(getAttributes, 
__FUNCTION__))(ref T value)

The problem with this is that __FUNCTION__ is a string when 
getAttributes expects a symbol. Can I somehow can a hold of the 
symbol of the caller.

Is this doable somehow?
May 18 2015
parent reply "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> writes:
On Monday, 18 May 2015 at 21:00:20 UTC, Per Nordlöw wrote:
 Is this doable somehow?
To clarify: Instead of *string* `__FUNCTION__` I instead want a reference to the *symbol* of the calling function scope typically passed as an alias parameter. We could of course always solve it with a mixin but that is 6+1 characters too many ;)
May 18 2015
parent reply "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> writes:
On Monday, 18 May 2015 at 21:04:19 UTC, Per Nordlöw wrote:
 To clarify: Instead of *string* `__FUNCTION__` I instead want a 
 reference to the *symbol* of the calling function scope 
 typically passed as an alias parameter. We could of course 
 always solve it with a mixin but that is 6+1 characters too 
 many ;)
I'm gonna try the tips here: http://forum.dlang.org/thread/mailman.160.1376790770.1719.digitalmars-d-learn puremagic.com
May 18 2015
parent reply "Per =?UTF-8?B?Tm9yZGzDtnci?= <per.nordlow gmail.com> writes:
On Monday, 18 May 2015 at 21:30:23 UTC, Per Nordlöw wrote:
 On Monday, 18 May 2015 at 21:04:19 UTC, Per Nordlöw wrote:
 To clarify: Instead of *string* `__FUNCTION__` I instead want 
 a reference to the *symbol* of the calling function scope 
 typically passed as an alias parameter. We could of course 
 always solve it with a mixin but that is 6+1 characters too 
 many ;)
I'm gonna try the tips here: http://forum.dlang.org/thread/mailman.160.1376790770.1719.digitalmars-d-learn puremagic.com
void yield(T)(ref T value) { mixin("alias caller = " ~ caller ~ ";"); } doesn't work across module boundaries not even for `__PRETTY_FUNCTION__`. Do we need need to fix the compiler, Walter?! ;)
May 18 2015
parent "anonymous" <anonymous example.com> writes:
On Monday, 18 May 2015 at 21:35:44 UTC, Per Nordlöw wrote:
     void yield(T)(ref T value)
     {
         mixin("alias caller = " ~ caller ~ ";");
     }

 doesn't work across module boundaries not even for 
 `__PRETTY_FUNCTION__`.

 Do we need need to fix the compiler, Walter?! ;)
You have to import the module, too: ---- void yield(T, string caller_str = __FUNCTION__)(ref T value) { import std.array: join, split; enum module_str = caller_str.split(".")[0 .. $ - 1].join("."); mixin("static import " ~ module_str ~ ";"); mixin("alias caller = " ~ caller_str ~ ";"); } ---- This fails for methods, of course. I guess you could remove elements from the back of __FUNCTION__ until it compiles as a module, and then import that.
May 18 2015