printf() metaprogramming challenge

Petar Petar
Fri May 24 08:00:31 UTC 2019


On Friday, 24 May 2019 at 07:09:56 UTC, Radu wrote:
> On Friday, 24 May 2019 at 04:49:22 UTC, bpr wrote:
>> On Thursday, 23 May 2019 at 19:33:15 UTC, Walter Bright wrote:
>>> string formatString(string f, string[] A ...)
>>> {
>>>     string r;
>>>     size_t i;
>>>     size_t ai;
>>>     while (i < f.length)
>>>     {
>>> 	if (f[i] != '%' || i + 1 == f.length)
>>> 	{
>>> 	    r ~= f[i];
>>
>>
>> Are you sure this works for betterC? It's been a while for me, 
>> but I think it won't, the string appends will stop it.
>>
>> Good job at getting unit tests and final switch in!
>>
>>> ----- End Of Das Code ----------
>
> Indeed it doesn't work with -betterC flag. Easily testable on 
> run.dlang.io
>
> This probably would work if CTFE was supported when compiling 
> with betterC.


In cases like this, one needs to use the enum lambda trick:

// Before:
string foo(string arg1) { /* .. */ }

// After:
enum foo(string arg1) = () { /* .. */ };


(Replace `string arg1` with all compile-time and run-time 
parameters that `foo` may take.)

That way, `foo` won't reach the code-generator and hence you 
won't get errors with `-betterC`.


More information about the Digitalmars-d mailing list