digitalmars.D.learn - Retrieving call expression of a function
- Quentin Ladeveze (17/17) Nov 28 2015 Hi,
- tcak (8/25) Nov 28 2015 I do not have right now to provide you with code, but three
- Quentin Ladeveze (27/58) Nov 28 2015 Thanks, it was a cool idea, I made something that works, but you
- tcak (7/68) Nov 28 2015 mixin template could solve this problem as well I guess. It
- Quentin Ladeveze (3/11) Nov 28 2015 I don't think mixin templates would work here. The reference says
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
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) " ThanksI 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
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: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.2Hi, 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) " ThanksI 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
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: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!"...";On Saturday, 28 November 2015 at 15:02:32 UTC, Quentin Ladeveze wrote: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.2Hi, 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) " ThanksI 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
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:I don't think mixin templates would work here. The reference says that we can only do declarations in a mixin template.[...]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