www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [suggestion] importing and renaming by wildcard and/or regexp

reply AnimusPEXUS <animuspexus protonmail.com> writes:
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
Jul 21 2022
next sibling parent Hipreme <msnmancini hotmail.com> writes:
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
Jul 27 2022
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
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: ```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`.
Jul 27 2022
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Aug 10 2022