Type-Safe template wrapper for AA#7924
Conversation
|
Thanks for your pull request and interest in making D better, @marler8997! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
a8dcd88 to
9d42f42
Compare
wilzbach
left a comment
There was a problem hiding this comment.
Nice. I was about to add this too, but you beat me to it :)
| V opIndex(const(K) key) | ||
| { | ||
| return cast(V)dmd_aaGetRvalue(aa, cast(void*)key); | ||
| } |
There was a problem hiding this comment.
How about adding opIndexAssign too?
auto opIndexAssign(V value, K key)
{
auto ptr = dmd_aaGet(&aa, cast(void*) key);
*ptr = cast(void*) value;
return *ptr;
}There was a problem hiding this comment.
Currently no code uses it this way and I don't anticipate code ever doing this.
aa[key] = value;
This being the more "intuitive" usage, I have to assume that Walter deliberately chose not to support this.
There was a problem hiding this comment.
maybe it's just because it wasn't implemented? maybe ask @WalterBright ?
There was a problem hiding this comment.
I don't think it's something worth bugging him about. Currently there's no use for it. We can always add it later if a use comes to light.
There was a problem hiding this comment.
Keep in mind that the current code base was translated from C+ (not a typo). I don't think there was any deliberate effort by Walter to avoid it.
There was a problem hiding this comment.
I'm also basing this on comments he made about one of @ibuclaw's PRs where he made a change that ended up causing multiple lookups of the same key. The current implementation uses getLvalue which allows the developer to do multiple operations on a value with only one lookup. I think the Walter designed the API to encourage this behavior, and adding opIndexAssign would provide a way to circumvent it, making it easy for developers to accidentally write code that performs multiple lookups that aren't necessary.
There was a problem hiding this comment.
Here's Walter's comment: #7696 (comment)
src/dmd/root/aav.d
Outdated
| { | ||
| return cast(V)dmd_aaGetRvalue(aa, cast(void*)key); | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
How about adding a public example:
///
unittest
{
AssocArray!(string, int) aa;
assert(aa["foo"] == 0);
aa["foo"] = 2;
assert(aa["foo"] == 2);
assert(aa.length == 1);
}16d7b3d to
b51e83f
Compare
wilzbach
left a comment
There was a problem hiding this comment.
Nice. I added this to the "no objection -> merge" queue.
|
Whats the purpose with this addition? |
|
I thought the purpose was self evident from the change (and the title)...what do you think? |
|
|
No long term goal in mind for this change, the change is its own end goal, a type safe template wrapper around AA. |
|
This has nothing to do with druntime. It's about introducing more type safety into the dmd codebase which uses its own AA-implementation back from the C++ times. I think what you are looking for is something like dlang/druntime#1282 |
|
Ahh, now I get it. Thanks, @wilzbach, I thought I was looking at a druntime PR... my mistake. |
No description provided.