-
-
Notifications
You must be signed in to change notification settings - Fork 753
Enable getSymbolsByUDA to retrieve private members. #3827
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /** | ||
| For testing only. | ||
| Provides a struct with UDA's defined in an external module. | ||
| Useful for validating behavior with member privacy. | ||
| */ | ||
| module std.internal.test.uda; | ||
|
|
||
| enum Attr; | ||
|
|
||
| struct HasPrivateMembers | ||
| { | ||
| @Attr int a; | ||
| int b; | ||
| @Attr private int c; | ||
| private int d; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yikes, this does not work. Not all type names are correctly printable and you always use an alias instead of a mixin string to get them.
Could we somehow replace this with
__traits(getMember, Type, name)?Or maybe we have to change the API to return a bunch of strings like
__traits(allMembers, Type)and leave it to the people to access the fields.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.
Only if we change the API to only retrieve publically accessible members (even if those members are visible in the caller's module)
I don't think this matters as we'd still be using a mixin for
hasSpecificUDA. Get rid of that mixin, and we can only support members visible tostd.traits.I'm not completely sure I understand the problem here, can you elaborate or give an example?
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.
Oh, there are deprecation warnings even with the code as-is. I think we'll have to give up on exposing members that
std.traitscan't access. Which could really limit the usefulness of this. Users will have to implement their owngetSymbolsByUDAin every module they want to use it (if they want it to return non-public members with the UDA).Uh oh!
There was an error while loading. Please reload this page.
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
__traits(getMember)should bypass visibility checks, b/c it's a meta programming tool and similar use cases are needed in many places.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.
Maybe I mixed that up with using formatted strings to rebuild the type, which doesn't work in many places.
But here you're just accessing the members of sth. which are always named.
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.
A mixin template might be viable alternative as well.
Uh oh!
There was an error while loading. Please reload this page.
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'm not sure this is the best idea.
__traits(getMember, x, "foo")is as easy to type asx.fooand can have disastrous consequences (if you mess with private data, you can screw up the expected invariants for a type).I think the best mechanism is to allow access to private symbols if you have the alias already. That is, I can alias my private symbol, hand it off to some other module, and because it now has that key, it can unlock the data. This allows the module to retain control over private data.
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'd like this as well, but it won't be useful in this particular case.
getSymbolsByUDAonly gets the 'parent' symbol, so we still wouldn't be able to access private child symbols.I think that would work, but it is a bit uglier on the calling end.