July 23, 2013 Re: Can't use variadic arguments to functions that use templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | On 07/23/2013 09:22 AM, JS wrote: > On Tuesday, 23 July 2013 at 16:15:03 UTC, Jesse Phillips wrote: >> On Tuesday, 23 July 2013 at 14:03:01 UTC, JS wrote: >>> I don't think you understand(or I've already got confused)... >>> >>> I'm trying to use B has a mixin(I don't think I made this clear). I >>> can't use it as a normal function. e.g., I can't seem to do >>> mixin(B(t)). If I could, this would definitely solve my problem. >> >> I'll stick with the reduced example, maybe you can apply it to the >> real world: >> >> template B(T...) { >> string B(T b) { >> string s; >> foreach(i, Type; T) >> s ~= Type.stringof ~ " " ~ b[i] ~ ";\n"; >> return s; >> } >> } >> >> void main() { >> enum forced = B("x", "a", "b"); >> pragma(msg, forced); >> mixin(forced); >> } > > What good does that do? Makes a string, mixes it into the source code and then compiles it. > What if I want to use a run-time variable in the mix? Just pass the runtime values to the template: import std.stdio; void main(string[] args) { if (args.length > 2) { writeln(B(args[1], args[2])); } } $ ./deneme abc xyz string abc; string xyz; That is pretty amazing that the same function can be called at compile time and runtime. Ali |
July 23, 2013 Re: Can't use variadic arguments to functions that use templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | On Tuesday, 23 July 2013 at 16:22:38 UTC, JS wrote:
> On Tuesday, 23 July 2013 at 16:15:03 UTC, Jesse Phillips wrote:
>> On Tuesday, 23 July 2013 at 14:03:01 UTC, JS wrote:
>>> I don't think you understand(or I've already got confused)...
>>>
>>> I'm trying to use B has a mixin(I don't think I made this clear). I can't use it as a normal function. e.g., I can't seem to do mixin(B(t)). If I could, this would definitely solve my problem.
>>...[Code]...
> What good does that do?
>
> What if I want to use a run-time variable in the mix?
Ah, I understand now you're interested in:
string concat(alias Data...)() { ... }
This does not exist, but maybe it should... I don't know of a workaround to get this behavior.
|
July 23, 2013 Re: Can't use variadic arguments to functions that use templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On Tue, Jul 23, 2013 at 08:54:12PM +0200, Jesse Phillips wrote: > On Tuesday, 23 July 2013 at 16:22:38 UTC, JS wrote: > >On Tuesday, 23 July 2013 at 16:15:03 UTC, Jesse Phillips wrote: > >>On Tuesday, 23 July 2013 at 14:03:01 UTC, JS wrote: > >>>I don't think you understand(or I've already got confused)... > >>> > >>>I'm trying to use B has a mixin(I don't think I made this > >>>clear). I can't use it as a normal function. e.g., I can't > >>>seem to do mixin(B(t)). If I could, this would definitely > >>>solve my problem. > >>...[Code]... > >What good does that do? > > > >What if I want to use a run-time variable in the mix? > > Ah, I understand now you're interested in: > > string concat(alias Data...)() { ... } > > This does not exist, but maybe it should... I don't know of a workaround to get this behavior. Is this by any chance related to that other thread about compile-time optimized join()? 'cos if it is, I've already solved the problem via templates: import std.stdio; template tuple(args...) { alias tuple = args; } /** * Given a tuple of strings, returns a tuple in which all adjacent compile-time * readable strings are concatenated. */ template tupleReduce(args...) { static if (args.length > 1) { static if (is(typeof(args[0])==string) && __traits(compiles, { enum x = args[0]; })) { static if (is(typeof(args[1])==string) && __traits(compiles, { enum x = args[1]; })) { alias tupleReduce = tupleReduce!(args[0] ~ args[1], args[2..$]); } else { alias tupleReduce = tuple!(args[0], args[1], tupleReduce!(args[2..$])); } } else { alias tupleReduce = tuple!(args[0], tupleReduce!(args[1..$])); } } else { alias tupleReduce = args; } } void main() { string x = "runtime1"; string y = "runtime2"; auto arr = [ tupleReduce!("a", "b", x, "c", "d", y, "e", "f", "g", x) ]; writeln(arr); } The output is: ["ab", "runtime1", "cd", "runtime2", "efg", "runtime1"] All compile-time readable strings in the list have been concatenated at compile-time. T -- I don't trust computers, I've spent too long programming to think that they can get anything right. -- James Miller |
July 23, 2013 Re: Can't use variadic arguments to functions that use templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Tuesday, 23 July 2013 at 19:14:26 UTC, H. S. Teoh wrote:
> On Tue, Jul 23, 2013 at 08:54:12PM +0200, Jesse Phillips wrote:
>> On Tuesday, 23 July 2013 at 16:22:38 UTC, JS wrote:
>> >On Tuesday, 23 July 2013 at 16:15:03 UTC, Jesse Phillips wrote:
>> >>On Tuesday, 23 July 2013 at 14:03:01 UTC, JS wrote:
>> >>>I don't think you understand(or I've already got confused)...
>> >>>
>> >>>I'm trying to use B has a mixin(I don't think I made this
>> >>>clear). I can't use it as a normal function. e.g., I can't
>> >>>seem to do mixin(B(t)). If I could, this would definitely
>> >>>solve my problem.
>> >>...[Code]...
>> >What good does that do?
>> >
>> >What if I want to use a run-time variable in the mix?
>>
>> Ah, I understand now you're interested in:
>>
>> string concat(alias Data...)() { ... }
>>
>> This does not exist, but maybe it should... I don't know of a
>> workaround to get this behavior.
>
> Is this by any chance related to that other thread about compile-time
> optimized join()? 'cos if it is, I've already solved the problem via
> templates:
>
> import std.stdio;
>
> template tuple(args...) {
> alias tuple = args;
> }
>
> /**
> * Given a tuple of strings, returns a tuple in which all adjacent compile-time
> * readable strings are concatenated.
> */
> template tupleReduce(args...)
> {
> static if (args.length > 1)
> {
> static if (is(typeof(args[0])==string) &&
> __traits(compiles, { enum x = args[0]; }))
> {
> static if (is(typeof(args[1])==string) &&
> __traits(compiles, { enum x = args[1]; }))
> {
> alias tupleReduce = tupleReduce!(args[0] ~
> args[1], args[2..$]);
> }
> else
> {
> alias tupleReduce = tuple!(args[0], args[1],
> tupleReduce!(args[2..$]));
> }
> }
> else
> {
> alias tupleReduce = tuple!(args[0],
> tupleReduce!(args[1..$]));
> }
> }
> else
> {
> alias tupleReduce = args;
> }
> }
>
> void main() {
> string x = "runtime1";
> string y = "runtime2";
> auto arr = [ tupleReduce!("a", "b", x, "c", "d", y, "e", "f", "g", x) ];
> writeln(arr);
> }
>
> The output is:
>
> ["ab", "runtime1", "cd", "runtime2", "efg", "runtime1"]
>
> All compile-time readable strings in the list have been concatenated at
> compile-time.
>
>
> T
Thanks, I'll check it out when I get a chance and report back. It
looks like it solves the problem.
|
Copyright © 1999-2021 by the D Language Foundation