Compile time values & implicit conditional mixin, as an alternative to tertiary operator hell and one-compile-time functions.

Stefan Koch uplink.coder at googlemail.com
Sat Jan 16 15:17:04 UTC 2021


On Saturday, 16 January 2021 at 14:56:18 UTC, Sebastiaan Koppe 
wrote:
> On Saturday, 16 January 2021 at 14:20:19 UTC, Paul wrote:
>> On Saturday, 16 January 2021 at 04:13:12 UTC, Paul Backus 
>> wrote:
>
> you can also use CTFE. Generate the values in a normal 
> function, assign it to an enum.
>
> ---
> string generateValues(uint L)() {
>     string result = "value.x";
>     if (L != 1) { result ~= ",value.y"; }
>     if (L != 2) { result ~= ",value.z"; }
>     if (L != 3) { result ~= ",value.w"; }
>     return result;
> }
>
> enum string values = generateValues!3();
>
> pragma(msg, values);
>
> void main(){};
> ---

That's not a good pattern

string generateValues(uint L)() should be
string generateValues(uint L)
and the invocation:
enum string values = generateValues!3();
should be
enum string values = generateValues(3);

What you are doing here is an expensive template instantiation 
for no reason.



More information about the Digitalmars-d mailing list