www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Retrieving call expression of a function

reply Quentin Ladeveze <ladeveze.quentin openmailbox.org> writes:
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
Nov 28 2015
parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
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.
Nov 28 2015
parent reply Quentin Ladeveze <ladeveze.quentin openmailbox.org> writes:
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
Nov 28 2015
parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
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!"...";
Nov 28 2015
parent Quentin Ladeveze <ladeveze.quentin openmailbox.org> writes:
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.
Nov 28 2015