New __traits: isModule and isPackage#5290
Conversation
src/traits.d
Outdated
There was a problem hiding this comment.
Don't use Phobos utilities in dmd code.
There was a problem hiding this comment.
Makes sense. What does DMD normally use for string concatenation? Should I just use strcat?
There was a problem hiding this comment.
printf followed by assert(0), inside an if.
There was a problem hiding this comment.
That should work. I'll probably use fprintf(stderr, ...), though.
There was a problem hiding this comment.
Better to follow the existing style, which prints to stdout.
There was a problem hiding this comment.
Hmm. That's a little odd. I guess it works, though. Fixed.
test/runnable/traits.d
Outdated
There was a problem hiding this comment.
We try not to import phobos inside the dmd test suite either.
There was a problem hiding this comment.
I was looking for something that had some "plain" packages plus modules and "package.d", but I suppose a cleaner solution would be to make a module in test/runnable/imports. I'll fix that.
src/traits.d
Outdated
There was a problem hiding this comment.
error("Internal Compiler Error: your message %s", imp.toChars()); ?
There was a problem hiding this comment.
That might work. I thought the error() function needed a Loc object, though. I'm not sure how to get one in this context.
Edit: oh, duh. e.loc should work. I think...
There was a problem hiding this comment.
Or just Loc() for no location.
|
I may have rebased this a bit prematurely, but it's rebased to master now. |
|
Looks meaningful. @WalterBright ? |
|
Fixed the merge conflict and rebased again. |
|
Is approval by @WalterBright the only thing holding this PR back? There's no rush; I just want to make sure there's nothing I need to fix. (Well, and bump this PR so it doesn't get buried forever ;)...) |
Could you show some real-world use-cases? Also, an associated bugzilla entry should be linked to (create one if it doesn't exist). Thanks. |
The use case that prompted this was the refactoring of my old Bullet Physics bindings. Originally, it used a kludgy build process that looped over the source files and created a new D program that would iterate over all the packages and pull out relevant symbols that needed "glue" code to interact with C++. Being able to iterate over the packages directly at compile time would be a fair bit cleaner. With the improved C++ interop that has developed since I started this PR, it's probably not as relevant as it used to be, but I can see it still being useful for binding to other languages. I'll create a Bugzilla entry. Thanks for pointing that out. EDIT: here's the Bugzilla entry. |
|
Pretty solid use request! :) I'll take a look in the front-end to see if we can use a hashmap of some kind to avoid O(n) lookups. |
So basically a __traits(allPackages)? |
|
Sorry, I thought you introduced a linear lookup somewhere, I might have been wrong here. |
It should mostly be constant-time in this code. |
|
Anyway I don't find this is a feature that needs a whole lot of discussion, it's pretty simple. Could you rebase it against master? |
|
Done. Now we'll see if the auto-tester likes it... |
|
@WalterBright @andralex: this is a pretty low-impact feature request, can we get an a-ok for adding the two new traits? It would benefit the BulletD project (which is high on the list of wanted C++ bindings, right up there with QtD). |
|
@blm768: P.S. you're going to have to squash your commits. :) But let's wait for W&A's approval first and then I'll have another look at the code and merge it. |
|
What's the status of this? It would be very useful. |
34e275c to
bd60620
Compare
|
Just rebased again and squashed. |
ab065f4 to
6159360
Compare
|
Managed to find and fix another off-by-one error in my changes. Apparently my D is rusty or something. Anyway, it looks like something in my changes has broken some aspect of scope resolution inside the module test.compilable.test16002;
import imports.plainpackage.plainmodule;
static assert(!is(imports.plainpackage.plainmodule == package));I haven't managed to determine why yet, but what seems to be happening is that the result of |
|
It was decided to have both. |
|
@thewilsonator: Both the |
|
Both |
|
(I'm about to travel back home so it will be probably 2 days before I get back around to this. Do ping me if I forget!) |
6a803ec to
44c6685
Compare
|
Thanks, please squash the commits and this is good to go. |
|
Yep. With my current level of consciousness, that was about guaranteed to happen. I really shouldn't be allowed to code at this time of night. |
|
|
|
Sorry; I'm really hammering those poor CI servers with all these failed attempts. Thanks for your patience while I fumble through this. |
|
No problems, I hammer them frequently too. That you for your continued interest! |
|
Well, I figured I might as well get this finished after three years. ;) I'll try to get the changes to the spec hammered out in the next couple of days. |
|
Haha, thanks. |
Why was that? |
|
I don't remember, this was 4 years ago.
|
This pull request would add two new __traits: isModule and isPackage, which (unsurprisingly) allow one to determine whether a symbol represents a module and/or a package. These traits can be quite useful for introspection under certain circumstances, especially some types of code generation (i.e. bindings).
Spec change: dlang/dlang.org#2199