Proposal for new compiler built-ins: __CTX__ and __CONTEXT__

FeepingCreature feepingcreature at gmail.com
Fri Jun 23 04:46:47 UTC 2023


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.


More information about the Digitalmars-d mailing list