Thread overview
[suggestion] importing and renaming by wildcard and/or regexp
Jul 21, 2022
AnimusPEXUS
Jul 27, 2022
Hipreme
Jul 27, 2022
Paul Backus
Aug 11, 2022
Ali Çehreli
July 21, 2022

Why?: for example I have module bindbc.glfw - It have symbols like GLFW_KEY_* so I want to import only GLFW_KEY symbols.

additionally: "Renamed and Selective Imports" with renaming by using regexp groups, so for example if I want to import GLFW_KEY_(\w+) and rename those like so MY_KEY_$1

July 27, 2022

On Thursday, 21 July 2022 at 22:08:14 UTC, AnimusPEXUS wrote:

>

Why?: for example I have module bindbc.glfw - It have symbols like GLFW_KEY_* so I want to import only GLFW_KEY symbols.

additionally: "Renamed and Selective Imports" with renaming by using regexp groups, so for example if I want to import GLFW_KEY_(\w+) and rename those like so MY_KEY_$1

Soo this is a bit too much, it would complicate a lot development and it could open a lot of opportunities for nasty bugs, ideally, those key definitions should be in their own module.

About the massive renaming, if you ever used std.regex you would know how slow it is to build that, but much beyond that, this is an overcomplication that should be totally avoided, glfw uses C enum declarations, while you could just create a named enum and if you ever wanted, you could just alias it

July 27, 2022

On Thursday, 21 July 2022 at 22:08:14 UTC, AnimusPEXUS wrote:

>

Why?: for example I have module bindbc.glfw - It have symbols like GLFW_KEY_* so I want to import only GLFW_KEY symbols.

additionally: "Renamed and Selective Imports" with renaming by using regexp groups, so for example if I want to import GLFW_KEY_(\w+) and rename those like so MY_KEY_$1

You can try something like this:

struct MY_KEY
{
    template opDispatch(string name)
    {
        static import bindbc.glfw;
        mixin(`alias opDispatch = bindbc.glfw.GLFW_KEY_`, name, `;`);
    }
}

This will let you write MY_KEY.FOO and get an alias for GLFW_KEY_FOO.

August 10, 2022
On 7/27/22 06:41, Paul Backus wrote:

> ```d
> struct MY_KEY
> {
>      template opDispatch(string name)
>      {
>          static import bindbc.glfw;
>          mixin(`alias opDispatch = bindbc.glfw.GLFW_KEY_`, name, `;`);
>      }
> }
> ```
>
> This will let you write `MY_KEY.FOO` and get an alias for `GLFW_KEY_FOO`.

Yep! :) I experimented with the same method to erase manual name mangling of C APIs:

  auto opDispatch(string func, Args...)(Args args) {
    enum expr = format!q{
      return pl_%s(plotter, args);
    }(func);

    mixin(expr);
  }

Note: Yes, my use of format is likely unnecessarily slow.

For example, instead of the C style

  pl_flinewidth_r(plotter, 1);

I wrapped the pointer in a struct and could be more D-like:

  plotter.flinewidth_r(1);

Ali