Compile-time optimization

Yota yotaxp at thatGoogleMailThing.com
Tue Jul 23 15:38:34 PDT 2013


On Tuesday, 23 July 2013 at 20:11:21 UTC, JS wrote:
> Your code doesn't exactly do what I wanted but probably because 
> I wasn't clear... I think it is modifiable and solves the 
> problem I was having(again, assuming your code is correct).
>
> Note though that something like
>
> string x = "asd"
> join(x,...); // x is compile time known and should be joined at 
> compile time. Not sure if D is smart enough to get this one?
>
> When I get some time later I'll check your code out, what I 
> want to do is get something like
>
> string x = join(a, b, "a", "b"); to produce the same code as
> string x = a~b~"ab";
>
> (no looping using for each except for string arrays)
>
> Thanks!


Don't forget that tuples can store more than just types!  See 
this for instance.

   import std.stdio;

   typeof(Xs[0]) sum(Xs...)()
   {
       pragma(msg, Xs); // tuple(1, 2, 3)
       typeof(Xs[0]) y;
       foreach(x; Xs) y ~= x;
       return y;
   }

   int main() {
       auto n = sum!(1, 2, 3);
       enum m = sum!(1, 2, 3);

       pragma(msg, typeof(n)); // int
       writeln(n); // 6
       writeln(m); // 6

       return 0;
   }

Works great.  When I comment out the 'sum!("1", "2", "3")' and 
replace it with a literal '6', the compiled EXE is byte for byte 
exactly the same.  You can exchange the '+=' for '~=' and it will 
concatenate strings instead.  Of course, now all arguments MUST 
be known at compile time, but that's no surprise.

PS: OK, just noticed that you were hoping to mix run time and 
compile time arguments.  Oops....
Oh well, posting anyway because I think it's neat. lol

As for a~"B"~"C" compiling into a~"BC", I think we just need to 
work on constant folding in the compiler. (If it doesn't already 
do this anyway.)


More information about the Digitalmars-d mailing list