Retrieving call expression of a function
tcak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Nov 28 09:19:38 PST 2015
On Saturday, 28 November 2015 at 15:41:59 UTC, Quentin Ladeveze
wrote:
> On Saturday, 28 November 2015 at 15:22:51 UTC, tcak wrote:
>> On Saturday, 28 November 2015 at 15:02:32 UTC, Quentin
>> Ladeveze wrote:
>>> Hi,
>>>
>>> Is it possible to retrieve the calling expression of a
>>> function ? Something like that
>>>
>>> ---
>>> import std.stdio;
>>>
>>> void funcTest(int x, float y)
>>> {
>>> writefln(get_call());
>>> }
>>>
>>> void main()
>>> {
>>> float x = 0.2;
>>> funcTest(1+2, x+2);
>>> }
>>> ---
>>>
>>> output expected : " funcTest(1+2, x+2) "
>>>
>>> Thanks
>>
>> I do not have right now to provide you with code, but three
>> things:
>>
>> 1. Use of mixin,
>> 2. The function call to be written in a string,
>> 3. A wrapper that stores given function call string, saves it,
>> and mixin it.
>
> Thanks, it was a cool idea, I made something that works, but
> you can only call the function with literals, not with
> variables :
>
> ---
>
> import std.stdio;
>
> void main()
> {
> int x = 2;
>
> enum call = "funcTest(1, 0.2);";
> callPrinter!call;
> }
>
> template callPrinter(string call)
> {
> void callPrinter()
> {
> writeln(call);
> mixin(call);
> }
> }
>
> void funcTest(int x, float y)
> {
> writeln("called with ", x, " and ", y);
> }
>
> ---
>
> output :
> funcTest(2, 0.2);
> called with 2 and 0.2
mixin template could solve this problem as well I guess. It
would, instead of calling a function, directly inject the code
into where you call it. So, remove the callPrinter function, make
template a mixin template, and in main, call it like mixin
callPrinter!"...";
More information about the Digitalmars-d-learn
mailing list