ImportC define attribute

Quirin Schroll qs.il.paperinik at gmail.com
Wed Aug 28 21:25:47 UTC 2024


On Saturday, 24 August 2024 at 15:50:14 UTC, ryuukk_ wrote:
> […]
> What about an attribute?
> ```D
> version(X) @cDefine("ENABLE_X")
> import lib;
> ```
>
> What do you think?

Like that, it breaks existing grammar semantics. An attribute is 
also weird; a `pragma` feels much more natural to me. Idiomatic D 
would require `version` + `else`:
```D
version(X)
{
     pragma(define, "ENABLE_X")
     pragma(define, "X", "1" ~ "2")
     {
         import libA : a;
         import libB : b;
     }
}
else
{
     import libA : a
     import libB : b;
}
```

The argument to `define` must be a string literal (not an 
expression of type string) that contains exactly an identifier or 
an identifier followed by comma-separated identifiers in 
parentheses. The second argument is optional and can be any 
expression evaluating to a `const(char)[]` at compile-time.

The first requirement isn't necessary for implementing the 
feature, it just makes sure it's obvious what the code does. The 
restriction should be lifted, should a compelling use case be 
found.


More information about the dip.ideas mailing list