Proposal for new compiler built-ins: __CTX__ and __CONTEXT__

H. S. Teoh hsteoh at qfbox.info
Fri Jun 23 05:01:34 UTC 2023


On Fri, Jun 23, 2023 at 04:48:01AM +0000, FeepingCreature via Digitalmars-d wrote:
> 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!

+1, seconded.


T

-- 
What do you call optometrist jokes? Vitreous humor.


More information about the Digitalmars-d mailing list