Do string mixins work under -betterC?
DLearner
bmqazwsx123 at gmail.com
Wed Dec 17 14:28:18 UTC 2025
On Wednesday, 17 December 2025 at 14:18:22 UTC, Richard (Rikki)
Andrew Cattermole wrote:
>
> On 18/12/2025 3:15 AM, DLearner wrote:
>> On Wednesday, 17 December 2025 at 00:13:42 UTC, Richard
>> (Rikki) Andrew Cattermole wrote:
>> [...]
>>>>> string genStuff(string val) {
>>>>> return val ~ ";";
>>>>> }
>>>>>
>>>>> void myFunc() {
>>>>> mixin(genStuff("this"));
>>>>> }
>> [...]
>>>>> This won't work without a way to mark `genStuff` as CTFE
>>>>> only. Which doesn't exist currently.
>>>>>
>>>>> Compiler has to know that a given execution context is CTFE
>>>>> only otherwise disallowed.
>>>>
>>>> 1. Any proposals to mark a function as CTFE only?
>>>
>>> No, this is something that should've been done a while back.
>>>
>>> The main concern is that you can get a linker error if you
>>> don't emit a function that was called.
>>
>> Actually, in your example, don't see the problem.
>> 'mixin' operates to provide text to the compiler _prior to_
>> run time.
>> Therefore, even if 'genStuff()' has a run-time (as well as)
>> compile-time meaning,
>> the use of mixin should force the compile-time meaning to be
>> taken.
>> So no ambiguity?
>> DL
>
> genStuff will still compile into the binary.
>
> The error happens when it tries to do that, not when it tries
> to run it for the string mixin.
And a suitably placed 'if (__ctfe) {...} else {}' within genStuff
doesn't help?
Idea is to tell the compiler that compilation for execution is
entirely redundant.
DL
More information about the Digitalmars-d-learn
mailing list