Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 02, 2016 Replace (ie: substitute) a type in varadic args | ||||
---|---|---|---|---|
| ||||
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 |
August 02, 2016 Re: Replace (ie: substitute) a type in varadic args | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saurabh Das | 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, > Saurabh 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); } |
August 02, 2016 Re: Replace (ie: substitute) a type in varadic args | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Campbell | 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: >> [...] > > 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); > } 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); } |
August 02, 2016 Re: Replace (ie: substitute) a type in varadic args | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saurabh Das | 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:
>> [...]
>
> 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);
> }
PS: I'm unsure if this results in additional copying of arguments in the case where there are no bools in the arguments.
|
August 02, 2016 Re: Replace (ie: substitute) a type in varadic args | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saurabh Das | 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:
>> On Tuesday, 2 August 2016 at 08:16:48 UTC, Sean Campbell 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);
>> }
>
> PS: I'm unsure if this results in additional copying of arguments in the case where there are no bools in the arguments.
Even if it does, the optimiser should take care of it. If it doesn't file a bug report.
|
Copyright © 1999-2021 by the D Language Foundation