Bug 20081 – Add pragma(identifier, StringExp)

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-07-25T04:30:00Z
Last change time
2024-12-13T19:04:41Z
Assigned to
No Owner
Creator
Manu
Moved to GitHub: dmd#19602 →

Comments

Comment #0 by turkeyman — 2019-07-25T04:30:00Z
It's extremely common in reflection based code to generate declarations using mixin, and possibly in order of 80% of the time, those declarations have generated names. Since string mixin's must contain whole declarations, it's impossible to supply a generated function name without writing the entire body of the declaration inside of a string. This is really ugly and difficult to maintain. Function bodies written in strings lose: Syntax highlighting Auto-completion Step debugging Hover tooltip inspection Text mixins are really unenjoyable and I'm very tired of it, particularly when I have huge declaration that are only text because of the name. I suggest to add `pragma(identifier, StringExp)` which may appear anywhere that an identifier may appear. In my code, I expect this will reduce instances of string mixin by something close to 90%, and that creates much more reasonable, readable, and maintainable code, and the IDE can keep working like it's meant to. For example: string makeIdent(size_t n) { return "name" ~ n.to!string; } template T(size_t N) { void pragma(identifier, makeIdent(N))(Args...)(Args args) { ... } } Also useful in places where an entire expression would have had to be enclosed in text, like: int pragma(identifier, getName()) = expression + 1 * myThing.members[n].x;
Comment #1 by dfj1esp02 — 2019-07-25T08:36:28Z
If you need only a name, you can just generate an alias: mixin(q{ alias generatedName=staticName; }); Or forwarding: mixin(q{ void generatedName(){ staticName(); } }); void staticName() { //code here }
Comment #2 by turkeyman — 2019-07-25T17:00:20Z
It's possible that I might have thought of that... ;)
Comment #3 by gooberman — 2019-07-27T13:02:58Z
100% for this. There's a large chunk of code I can simplify by just defining things inline instead of having to provide extra workarounds like noted in all of the above posts. Being able to arbitrarily override a symbol's identifier is going to have applications well outside of just this use case.
Comment #4 by destructionator — 2019-08-05T00:59:50Z
I'd actually prefer the syntax just to be mixin. Change the grammar so declaration names can be replaced with mixin(x) and the mixin must return an identifier as a string. But regardless, yes, this would basically finish `static foreach` imo.
Comment #5 by turkeyman — 2019-08-05T07:06:38Z
mixin would be fine in that context... but it might look a little bit weird?
Comment #6 by robert.schadek — 2024-12-13T19:04:41Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19602 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB