digitalmars.D - [suggestion] importing and renaming by wildcard and/or regexp
- AnimusPEXUS (5/5) Jul 21 2022 Why?: for example I have module bindbc.glfw - It have symbols
- Hipreme (9/14) Jul 27 2022 Soo this is a bit too much, it would complicate a lot development
- Paul Backus (15/20) Jul 27 2022 You can try something like this:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (15/26) Aug 10 2022 Yep! :) I experimented with the same method to erase manual name
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
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_$1Soo 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
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_$1You 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
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