-
-
Notifications
You must be signed in to change notification settings - Fork 747
Issue 16485 - Add trait for testing if a member is static. #5112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No without the required change to the protection when certain __trait() verbs are used. Which is the case here.
std/traits.d
Outdated
| template isStaticMember(T, string member) | ||
| if (__traits(hasMember, T, member)) | ||
| { | ||
| import std.meta : Alias; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, it'll fail if the inspection is made in another module without a public protection.
To be clear anything like that shouldn't be proposed until something like this is implemented.
If you merge this without the new behavior against protection for __trait() then you'll get a bug report, certainly. Can't say when, but this will happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it causes that big of a problem that it's worth waiting for a DIP that's still being written. When or even if that DIP is introduced, it won't cause any breakage to existing code and it'll be a small patch to allow private protected symbols for isStaticMember. I don't see there being very many cases where it would need to be made known whether a private member is static in another module that it won't be able to access anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a DIP anymore, it'll be done without following the DIP process. The question is when.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #5112 (comment)
Doesn't look like they are going to be going with that exact DIP implementation, so changes won't be necessary.
|
Hmm not sure why @dlang-bot isn't linking the issue with bugzilla. |
You need to mention it with the git commit message. |
938fd8c to
8cdae1b
Compare
|
Well, I didn't think that there was really anything left to do on it. The only comment on there had to do with adding "returns" to the docs, which seems inappropriate to me, since it's not a function, but whatever.
I don't think that this particularly matters for this PR. Yes, the language needs to be fixed, but type introspection in general is broken until the language is fixed, and worst case, using this template just results in compilation errors when you use it on something with inaccessible symbols. Yes, it sucks, but there's no fix for it without fixing the language, and it's already been agreed that we're going to fix the language. So, this template will work on some code for now and fail to compile on other code, and then later, it'll start compiling on that code too. |
|
@sprinkle131313 this still needs a changelog entry. Please see the |
8cdae1b to
da3e959
Compare
|
@wilzbach https://github.com/dlang/phobos/tree/master/changelog |
da3e959 to
eee2785
Compare
Yeah nobody has merged this PR yet: dlang/dlang.org#1549 |
andralex
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, please rename to hasStaticMember for symmetry with hasMember. Thanks!
|
|
||
| void m() {} | ||
| final void m2() const pure nothrow @nogc @safe {} | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some of these are not covered; if they are needed as declarations but never called please don't define
This isn't testing whether the type has a member by that name that's static. It's testing whether a member that it definitely has is static or not. If it were going to test whether a type had a member and if it were static, then its implementation would need to be changed, and doing that would result in unnecessary redundancy, because the typical way to use this is likely to be when you have a list of members already and are checking which ones are static. |
|
Oh, then definitely something is off. If a member known to exist is to be verified, then |
I'm not very sure this is the case, what would be some scenarios? |
I believe that the reason this is the way it is is primarily because
Manu is the one who pushed for this originally, and I don't know exactly what his use case was, but he was specifically looking to test whether a member was static, not whether a type had a member with a particular name that was static. Personally, I've run into the need for a trait like this when dealing with serialization code (e.g. serializing a struct to JSON based on its members). In that case, you're definitely dealing with a list of members and testing whether they're static rather than trying to see if a type has a specific member that is static. I would think that most cases where you were looking to simply use a static member variable, you wouldn't care whether it was an enum or a static variable, and you could just check whether you could use the member in the context you wanted to, and if it's a static function you're looking for, The original thread (split for some reason): http://forum.dlang.org/post/mailman.810.1475501243.2994.digitalmars-d@puremagic.com |
If @sprinkle131313 you may want to rethink the design, thanks. |
What effort is already done? All you have from
It's a compiler error. The template constraint requires that the given string be a name of one of the members of the given type. If you used |
|
@jmdavis I'm saying the work has already been done to figure out whether it's a member, so it's difficult to justify it. I'd rather use |
|
Hmmm. So, basically, take the current implementation and move the template constraint to a static if? It would be the same test either way, so that does make sense. It is pretty weird from the standpoint of the original goal, since in that case, you already know that it's a member, but it still works, and since the template constraint was testing it anyway, there's no extra overhead for the original use case, and there's less overhead in the case where you don't know for sure that it's a member. Okay. That makes sense. |
f4a083c to
c85814e
Compare
std/traits.d
Outdated
| * $(LREF hasElaborateDestructor) | ||
| * $(LREF hasIndirections) | ||
| * $(LREF hasMember) | ||
| * $(LREF isStaticMember) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs to be changed, and your most recent commit has unrelated changes mixed in.
| struct S | ||
| { | ||
| static int globalVar; | ||
| int localVar; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
global and local should be nonstatic and static
.vscode/tasks.json
Outdated
| { | ||
| // See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
| // for the documentation about the tasks.json format | ||
| "version": "0.1.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's with this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My mistake, i hate that ctrl+enter commits everything even if it wasn't staged. I keep doing it by accident.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a new PR that adds these files to the .gitignore, s.t. you don't need to worry about it in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the .vscode and .lib files (that's why our CI fails).
Feel free to add them to the .gitignore instead.
|
|
||
| inout(int) iom() inout { return 10; } | ||
| static inout(int) iosf(inout int x); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't define a body when you don't call it as it will lead to coverage errors ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those ones caused link errors in the test build if they aren't there.
For reference, the related error'd build:
https://circleci.com/gh/dlang/phobos/2050
generated/linux/debug/64/unittest/libphobos2-ut.so: undefined reference to `_D3std6traits18__unittestL3606_69FZ1C1gMOFZv'
generated/linux/debug/64/unittest/libphobos2-ut.so: undefined reference to `_D3std6traits18__unittestL3606_69FZ1C1pMFNdZi'
generated/linux/debug/64/unittest/libphobos2-ut.so: undefined reference to `_D3std6traits18__unittestL3606_69FZ1C3iomMNgFZNgi'
generated/linux/debug/64/unittest/libphobos2-ut.so: undefined reference to `_D3std6traits18__unittestL3606_69FZ1C1mMFZv'
| @@ -0,0 +1,13 @@ | |||
| Added `std.traits.hasStaticMember` to check whether a symbol is a static member of a type. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andralex while the current changelog parser is very forgiving, we should probably make a decision on the format:
- (A) title (can be multiple lines) - empty line - description
- (B) title (one line) - empty line (optional) - description
(A) is implemented as dlang/tools#220
(B) is how our parser works currently.
Personally I am quite happy with closing the PR and agreeing that a title can't be more than one line (doesn't make much sense anyways).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll let @CyberShadow decide, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any deciding arguments one way or another.
I think enforcing that the second line is blank would be good to avoid accidental ambiguity, and would follow formatting for git commit messages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: start with a more restrictive policy then relax it as experience develops. The other way is much more difficult, as we've all learned the hard way :).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think enforcing that the second line is blank would be good to avoid accidental ambiguity,
Suggestion: start with a more restrictive policy then relax it as experience develops.
@sprinkle131313 as we seem to have reached a consensus here, would you be so kind to insert an empty line after the title?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wilzbach how do we use that nice relatively new feature of github allowing others to amend someone's pull request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR author needs to opt in, which it doesn't look like they've done here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://wiki.dlang.org/Guidelines_for_maintainers#Write_access_to_PRs
It's enabled here:
Anyways we put this into the reminder message of the DLang-Bot:
dlang/dlang-bot#44
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's enabled here:
Oh, oops, I was looking in the wrong place.
e0da18e to
d79da1d
Compare

Opening this new pull request as @jmdavis seems busy and hasn't replied to pings, credit to him (#4834)