injecting imports when compiling

WebFreak001 d.forum at webfreak.org
Tue Dec 7 09:54:40 UTC 2021


Hi, I think it would be interesting to have a compiler flag to 
inject imports into all compiling .d files.

This would be basically just extending the built-in implicit 
`import object;` that's in every non-betterC D file with any 
custom file you, or the build tool, might want to add.

Use cases:
- small application templates/modes with common imports set (like 
std.stdio, std.conv, std.algorithm, std.array, std.format) so you 
can more easily and quickly prototype (C# 10 / .NET 6 for example 
added this with implicit usings, though I'm not a fan because C# 
is just not that good at quick prototyping like D is)

- better DUB environment injection:
   - allow to pass in package version (usually built from git tag 
+ commit hash, or when fetched the actual version) - this 
currently needs workarounds by invoking some preBuildCommands, 
which may not work on every target machine (e.g. DCD, D-Scanner, 
dfmt invoking rdmd, which is usually installed, but for example 
not on linux distros that package each tool individually)
   - allow to define manifest constants for different features 
(like build config name, configuration name, passed in 
architecture/triple, custom defined strings from dub.json)
   - possibility to publish packages that could have a flag to 
inject into the global namespace, for very common stuff or 
debugging stuff like custom asserts
   - additionally to the `Have_xyz` versions we could define `enum 
string xyz_version = "1.2.3";` to allow dependencies to act 
differently based on package versions
     - we could additionally expose a `dubHave("packageName", 
"minVersion")` function for conditional compilation
   - expose build options, flags, requirements, toolchain info, 
etc.

- allows to define special UDAs as a kind of UDA stdlib that 
could be used for linting, auto completion hints, static analysis 
etc. Think of:
   - `@nullable` for reference types
   - standard `@suppressWarning("...")` for D-Scanner or other 
linters
   - common, interoperable `@optional`, `@required` UDAs for all 
serialization libraries (maybe as a library though)

Do you like this idea? Have any suggestions? Concerns?



More information about the Digitalmars-d mailing list