Implement issue# 16485. Add trait for testing if a member is static.#4834
Implement issue# 16485. Add trait for testing if a member is static.#4834jmdavis wants to merge 1 commit intodlang:masterfrom
Conversation
|
template isStaticMember(T, string member) if (__traits(hasMember, T, member)) Seems better |
std/traits.d
Outdated
|
|
||
| static if (__traits(isStaticFunction, sym)) | ||
| enum bool isStaticMember = true; | ||
| else static if (is(FunctionTypeOf!sym == function) && |
There was a problem hiding this comment.
wrap the other checks inside a static if(is(sym == function)) to avoid templates for the most common case.
992dbb8 to
1d9b6b9
Compare
|
Okay. I updated the code per the comments here and in the newsgroup. |
1d9b6b9 to
13b3bd3
Compare
| static if (is(typeof(sym) == function) || is(FunctionTypeOf!(typeof(&sym)) == function)) | ||
| enum bool isStaticMember = __traits(isStaticFunction, sym); | ||
| else | ||
| enum bool isStaticMember = __traits(compiles, &sym); |
There was a problem hiding this comment.
Why use compiles instead of is(typeof(&sym))?
There was a problem hiding this comment.
I'm not sure that it matters in this case, but there are subtle differences between the two, and I've been advised in the past to favor __traits(compiles, ...). If I understand correctly, the main difference is that __traits(compiles, ...) actually checks for compilation, whereas is(typeof(...)) just checks that the type exists (e.g. there was a discussion on it here with Don: https://issues.dlang.org/show_bug.cgi?id=8339 ). __traits(compiles, ...) is also more explicit about what you're doing. So, I generally try to use __traits(compiles, ...) instead of is(typeof(...)). I confess though that I don't understand the subtleties well enough to know when it really matters.
There was a problem hiding this comment.
Feels like a weird recommendation. I wonder if there is a compiler workload difference?
I have a very poor feel for what things the compiler does quickly, and which are high-impact on compile time.
There was a problem hiding this comment.
Andrei was the one who recommended the traits option to me, so you can probably ask him
There was a problem hiding this comment.
In cases where this does hit your compile times you need a high n.
In such cases template instanciation cost will shadow everything else.
There was a problem hiding this comment.
What does 'accurate' mean? I've seen a bunch of weird recommendations emerging... they're not written anywhere. Are people just supposed to know?
There was a problem hiding this comment.
@TurkeyMan It's more or less folk knowledge that's passed around. I don't think it's documented anywhere other than various forum threads. That said, it is definitely better to use __traits(compiles).
There was a problem hiding this comment.
It's more or less folk knowledge that's passed around.
Right, and that's absolutely not okay! We're meant to be attracting new users. The meta experience is D's main offering, and new users should be able to get working and feel powerful with as little friction as possible. "Oh, yeah that obvious thing to type isn't actually what you do, you need to do this awkward unintuitive thing instead" is not something that's okay to say to new users.
There was a problem hiding this comment.
I'm not claiming it's obvious or ideal 😉 Just letting you know how things are.
13b3bd3 to
7c81234
Compare
isStaticMember tests whether a member of a struct or class is static or not. It works with both functions and variables.
7c81234 to
e0ab406
Compare
|
I figured out how to make |
|
Needs a changelog entry |
|
Wait, |
http://dlang.org/spec/attribute.html#gshared: "__gshared may also be applied to member variables and local variables. In these cases, __gshared is equivalent to static, except that the variable is shared by all threads rather than being thread local." In contrast, |
Ok thanks. Also, what's the reason to use strings to do |
Because that's what you get from |
|
|
||
| /++ | ||
| Whether the symbol represented by the string, member, is a static member of | ||
| T. |
There was a problem hiding this comment.
Needs params and returns
This will be automatically generated if it's detected by the Dlang Bot. @jmdavis if you remove
this would allow it to do it's magic (auto-linking, closing, adding to the changelog):
|
|
Closing as @sprinkle131313 has "revived" this at #5112 |
isStaticMember tests whether a member of a struct or class is static or
not. It works with both functions and variables.
This has come up several times lately, and it definitely seems like we need something like this in std.traits. Technically, issue# 16485 is for adding a trait for testing for a static variable whereas this works with both functions and variables, but they should be distinguishable via
__traits(isStaticFunction, ...). The most recent discussion on this ishttp://forum.dlang.org/post/mailman.810.1475501243.2994.digitalmars-d@puremagic.com
The tests are a slightly expanded version of what Manu proposed there.