assert(__ctfe) and betterC

Petar Petar
Wed Jan 27 21:49:45 UTC 2021


On Wednesday, 27 January 2021 at 21:27:45 UTC, Steven 
Schveighoffer wrote:
> On 1/27/21 4:05 PM, Tim wrote:
>> On Wednesday, 27 January 2021 at 20:59:24 UTC, Steven 
>> Schveighoffer wrote:
>>> Is there another way I can do this?
>>>
>> 
>> One way I have sometimes used is an alias to a function 
>> literal:
>> 
>> alias generateVar = function(string name) {
>>      return "int " ~ name ~ ";";
>> };
>> 
>> mixin(generateVar("i"));
>> 
>> extern(C) int main()
>> {
>>      i = 0;
>>      return i;
>> }
>> 
>> This works with betterC.
>
> Well, that works great! What a nice... well, I have no idea why 
> this works. But thank you!
>
> -Steve

`enum` also works:

enum generateVar = (string name) => "int " ~ name ~ ";";

The key difference between these ways of defining a function and 
the regular way, is that with them it's guaranteed that the 
compiler won't codegen them since simply  it only does this for 
regular functions.

assert(__ctfe) could be made to work, but it's wrong way to go 
about things. Instead the compiler should only code-gen what is 
actually necessary.

In the posts below I have sketched out how a scheme for fully 
on-demand codegen might look like, based on `export` inference:

https://forum.dlang.org/post/xbllqrpvflazfpowizwj@forum.dlang.org
https://forum.dlang.org/post/nvvgrdlucajshjngdjlj@forum.dlang.org



More information about the Digitalmars-d mailing list