Proposal for new compiler built-ins: __CTX__ and __CONTEXT__

ryuukk_ ryuukk.dev at gmail.com
Fri Jun 23 14:36:48 UTC 2023


On Friday, 23 June 2023 at 04:46:47 UTC, FeepingCreature wrote:
> On Thursday, 22 June 2023 at 19:49:31 UTC, ryuukk_ wrote:
>> I like the idea, i have some code in my projects where i use 
>> __FILE__ and __LINE__, would make my functions much shorters
>>
>> However, i feel like this is asking for something else
>>
>> Like your example:
>>
>> ```D
>> void infoStack(string msg, __CTX__ ctx = __CONTEXT__) {
>>     writefln("%s(%s): %s", ctx.file, ctx.line, msg);
>> }
>> ```
>>
>> This is redundant, you'd never pass a __CTX__ yourself, i 
>> reusing traits as suggested trait makes more sense
>>
>> Never been a fan of uglyf-ing function arguments that one 
>> never gonna use themselves
>>
>>
>> ```D
>> void infoStack(string msg) {
>>     enum file = __traits(context, calling_file);
>>     enum line = __traits(context, calling_line);
>>     writefln("%s(%s): %s", file, line, msg);
>> }
>> ```
>
> There's reasons to pass a __CTX__ yourself. The classic is 
> unittest functions, where you want to propagate the context of 
> the user-facing callsite rather than any number of nested 
> functions of test code.
>
> Also your function has an ABI that depends on inspecting the 
> function body. That's not a good idea. For one, it completely 
> breaks .di files.


I also value code aesthetics, bunch of extra function arguments 
uppercase with ugly _ everywhere is tasteless

I think there is value in what i propose, you can still pass 
arguments if your special case needs it, but by default it allows 
clean and nice functions




More information about the Digitalmars-d mailing list