Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 28, 2015 Retrieving call expression of a function | ||||
---|---|---|---|---|
| ||||
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 |
November 28, 2015 Re: Retrieving call expression of a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Quentin Ladeveze | 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.
|
November 28, 2015 Re: Retrieving call expression of a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | 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
|
November 28, 2015 Re: Retrieving call expression of a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Quentin Ladeveze | 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!"...";
|
November 28, 2015 Re: Retrieving call expression of a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | On Saturday, 28 November 2015 at 17:19:40 UTC, tcak wrote:
> On Saturday, 28 November 2015 at 15:41:59 UTC, Quentin Ladeveze wrote:
>> [...]
>
> 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!"...";
I don't think mixin templates would work here. The reference says that we can only do declarations in a mixin template.
|
Copyright © 1999-2021 by the D Language Foundation