injecting imports when compiling

bauss jj_1337 at
Wed Dec 8 07:46:20 UTC 2021

On Tuesday, 7 December 2021 at 09:54:40 UTC, WebFreak001 wrote:
> 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?

Another thing that would be great would be something like "import 
*;" which would import all modules in the current compilation 
unit, since traits doesn't work very well on all types in a 
package and you often can't retrieve all symbols, so being able 
to import all modules will ensure all symbols are available in a 
specific module.

This is very useful when autogenerating code based on modules 
located in multiple different packages etc.

Or at least something like what you suggested that allows to 
specific a module that has all modules imported per standard 
through command-line as you've shown, of course with a flag that 
allows exclusion of certain modules/packages ex. you probably 
would want to exclude the standard library.

More information about the Digitalmars-d mailing list