d - Compile time evaluation -


if write

enum chars = digits ~ uppercase; 

will string concatenated @ compile time? i'm assuming will. if replace string literal or ctfe function can't measure significant performance differences (even calling hundred million times). difference if replace enum const. i've been told it's inefficient write this. thought kind of convenient , don't see inefficiency. (btw, line in function that's called recursively).

the full code (converting numeral system different base)

import std.string;  string tobase(long n, int b)  in {     assert(2 <= b && b <= 35); } body {     static string sign;     if (n < 0) {         n *= -1;         sign = "-";     }     enum chars = digits ~ uppercase;     size_t r = cast(size_t)(n % b);     if (n == r) {         return sign ~ chars[r];     }     return tobase((n - r) / b, b) ~ chars[r]; } 

edit: updated code, in response comments, not relevant question

string tobase(long n, int b)  in {     assert(2 <= b && b <= 35); } body {     enum chars = digits ~ uppercase;     long r = n % b;     char c = chars[cast(size_t) abs(r)];     if (n == r) {         return (n < 0 ? "-" : "") ~ c;     }     return tobase((n - r) / b, b) ~ c; } 

enum instantiations always evaluated @ compile-time (and throw compile errors when evaluation impossible @ compile time)

so concatenation done @ compile-time , immutable version stored in code , referenced @ runtime


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -