www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - ImportC define attribute

reply ryuukk_ <ryuukk.dev gmail.com> writes:
D makes it possible to import C files transparently and "it just 
works"

According to walter, users should be doing this when wanting to 
pass a version conditional to C files:

`dmd -i -version=ENABLE_X -P=-DENABLE_X app.d`

I find this just bad, i shouldn't have to repeat anything, it is 
error prone and makes build scripts an unreadable mess, specially 
if you have multiple -version

One other suggestion was to split my files.. worse..


Well then, how are other languages managing that?

Zig for example:

```zig
const c =  cImport({
     if (X) {
          cDefine("ENABLE_X");
     }
      cInclude("lib.h");
});
```

Wow very nice, they thought about it beforehand, they gives you 
control so it can be nicely integrated with your other Zig code! 
great, no wonder it is a popular language, it has nice UX

D should match that great user experience to avoid walter's bad 
status quo

What about an attribute?


```D
version(X)  cDefine("ENABLE_X")
import lib;
```

API would be:
```D
struct cdefine
{
     string key;
     string value; // optional
}
```

What do you think?
Aug 24
parent reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
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.
Aug 28
parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Wednesday, 28 August 2024 at 21:25:47 UTC, Quirin Schroll 
wrote:
 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.
I like this idea of using a `pragma` instead I don't like the version/else D should lean towards conciseness, not javaness I refuse, i literally refuse to type these kind of messy code, i'll challenge the status quo until i end up switching language Because so far, it leans towards cementing the status quo, wich is sad
Aug 29