digitalmars.D.learn - Replace (ie: substitute) a type in varadic args
- Saurabh Das (18/18) Aug 02 2016 How can I substitute the type of an argument received via a
- Sean Campbell (29/47) Aug 02 2016 Just of the top of my head, using ugly string mixins, this:
- Saurabh Das (8/38) Aug 02 2016 Thanks. Yes that is one approach. I figured out another approach
- Saurabh Das (3/13) Aug 02 2016 PS: I'm unsure if this results in additional copying of arguments
- Nicholas Wilson (3/18) Aug 02 2016 Even if it does, the optimiser should take care of it. If it
How can I substitute the type of an argument received via a varadic template? For example say I want to generalise this scenario: auto myConverterFunction1(bool arg1, bool arg2, ubyte arg3, int arg4) { return targetFunction(cast(ubyte)arg1, cast(ubyte)arg2, arg3, arg4); } So I'll have something like: auto myConverterFunction2(Args...)(Args args) { // Don't know how to do this part... } Basically I need to substitute bool for ubyte to interface with the Java JNI. Thanks, Saurabh
Aug 02 2016
On Tuesday, 2 August 2016 at 07:24:28 UTC, Saurabh Das wrote:How can I substitute the type of an argument received via a varadic template? For example say I want to generalise this scenario: auto myConverterFunction1(bool arg1, bool arg2, ubyte arg3, int arg4) { return targetFunction(cast(ubyte)arg1, cast(ubyte)arg2, arg3, arg4); } So I'll have something like: auto myConverterFunction2(Args...)(Args args) { // Don't know how to do this part... } Basically I need to substitute bool for ubyte to interface with the Java JNI. Thanks, SaurabhJust of the top of my head, using ugly string mixins, this: auto myConverterFunc(Args...)(Args args) { string genCode() { string code = "targetFunction("; foreach (i, Arg; Args) { static if (is(Arg == bool)) code ~= format("cast(ubyte)args[%s]%s", i, i == Args.length ? "" : ","); else code ~= format("args[%s]%s", i, i == Args.length ? "" : ","); } code ~= ");"; return code; } mixin(genCode()); } void targetFunction(ubyte i, ubyte j, uint k, int l) { writefln("i : %s, j : %s, k : %s, l : %s",i,j,k,l); } void main() { myConverterFunc(true,false,10,20); }
Aug 02 2016
On Tuesday, 2 August 2016 at 08:16:48 UTC, Sean Campbell wrote:On Tuesday, 2 August 2016 at 07:24:28 UTC, Saurabh Das wrote:Thanks. Yes that is one approach. I figured out another approach that seems decent: auto targetFunctionProxy(Args...)(Args args) { import std.meta; return targetFunction!(ReplaceAll!(bool, ubyte, Args))(args); }[...]Just of the top of my head, using ugly string mixins, this: auto myConverterFunc(Args...)(Args args) { string genCode() { string code = "targetFunction("; foreach (i, Arg; Args) { static if (is(Arg == bool)) code ~= format("cast(ubyte)args[%s]%s", i, i == Args.length ? "" : ","); else code ~= format("args[%s]%s", i, i == Args.length ? "" : ","); } code ~= ");"; return code; } mixin(genCode()); } void targetFunction(ubyte i, ubyte j, uint k, int l) { writefln("i : %s, j : %s, k : %s, l : %s",i,j,k,l); } void main() { myConverterFunc(true,false,10,20); }
Aug 02 2016
On Tuesday, 2 August 2016 at 08:20:22 UTC, Saurabh Das wrote:On Tuesday, 2 August 2016 at 08:16:48 UTC, Sean Campbell wrote:PS: I'm unsure if this results in additional copying of arguments in the case where there are no bools in the arguments.[...]Thanks. Yes that is one approach. I figured out another approach that seems decent: auto targetFunctionProxy(Args...)(Args args) { import std.meta; return targetFunction!(ReplaceAll!(bool, ubyte, Args))(args); }
Aug 02 2016
On Tuesday, 2 August 2016 at 08:22:15 UTC, Saurabh Das wrote:On Tuesday, 2 August 2016 at 08:20:22 UTC, Saurabh Das wrote:Even if it does, the optimiser should take care of it. If it doesn't file a bug report.On Tuesday, 2 August 2016 at 08:16:48 UTC, Sean Campbell wrote:PS: I'm unsure if this results in additional copying of arguments in the case where there are no bools in the arguments.[...]Thanks. Yes that is one approach. I figured out another approach that seems decent: auto targetFunctionProxy(Args...)(Args args) { import std.meta; return targetFunction!(ReplaceAll!(bool, ubyte, Args))(args); }
Aug 02 2016