Proposal for new compiler built-ins: __CTX__ and __CONTEXT__

Tim tim.dlang at t-online.de
Fri Jun 23 18:27:33 UTC 2023


On Thursday, 22 June 2023 at 17:18:25 UTC, Andrej Mitrovic wrote:
> 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.

This could be implemented in a library after 
https://issues.dlang.org/show_bug.cgi?id=18919 is fixed. An 
implementation could then look like this:

```
struct Loc
{
    string file;
    size_t line;
}
Loc defaultLoc(string file = __FILE__, size_t line = __LINE__)
{
    return Loc(file, line);
}
void foo(Loc loc = defaultLoc)
{
    writeln(loc.file, " ", loc.line);
}
```

Currently it prints the wrong location, because `__FILE__` and 
`__LINE__` are not evaluated at the real call site, but that 
could be changed.

A library implementation would have the advantage that you could 
choose the members.


More information about the Digitalmars-d mailing list