Proposal for new compiler built-ins: __CTX__ and __CONTEXT__

FeepingCreature feepingcreature at gmail.com
Fri Jun 23 04:48:01 UTC 2023


On Thursday, 22 June 2023 at 17:18:25 UTC, Andrej Mitrovic wrote:
> This isn't a DIP but I'd like to start this conversation to see 
> what people think.
>
> ...
> 
> I'm envisioning a new type `__CTX__` which contains all of 
> these different contexts which are currently separate keywords 
> on: https://dlang.org/spec/expression.html#specialkeywords.
>
> Here it is:
>
> ```D
> struct __CTX__ {
>     string file;
>     string file_full_path;
>     int line;
>     string func;
>     string pretty_func;
> }
> ```
>
> Having it defined as a struct serves a few objectives:
> - It makes it easy to declare and use this type in user code.
> - All library code will have one single compatible type they 
> can easily pass around to each other.
> - Makes it harder to confuse parameters. For example it's 
> currently easy to pass a random string to a function expecting 
> a `__FILE__`, because the parameter is a string.
> - It makes it possible to choose whether to receive and pass 
> this structure around by value or by reference.
>
> We also need to initialize it. So perhaps we'd call this 
> initialization keyword `__CONTEXT__`.
I love it!


More information about the Digitalmars-d mailing list