-
-
Notifications
You must be signed in to change notification settings - Fork 422
Conversation
|
See this comment, I think these should be easy to support |
|
I suggest to add the std.range.byTuple function too. |
|
I think this proposal allows to access fields with .key and .value field names: But I think hypotetical future proposals for built-in tuples will not allow names for tuple fields. So at that point if byPair will be modified to return a built-in tuple, the user-code that uses the .key and .value field names will be broken. A more future-proof solution seems to allow only t[0] and t[1] access to the fields of a byPair pair. |
|
Why wouldn't future proposals for tuples not allow names for tuple fields? That would be a regression from the current |
|
@schveiguy I'm not sure how "easy" it is; sure it's easy to code, but it's as ugly as sin. The main reason is that the current implementation of Anyway, I've left |
|
@quickfur: I am not sure, but I think the current Tuple proposal+patch by Kenji (DIP32) doesn't include field names. And many built-in tuple implementations (like Haskell or Python ones) don't have such field names. Usually tuples with field names are named records... |
|
Yeah, please remove the |
And how do you get ref access to the value if you return a tuple only? I think we are forgetting the power of D features. |
|
|
||
| enum length = 2U; | ||
| @property ref Key key() { return *keyp; } | ||
| @property ref Value value() { return *valp; } |
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 these are redundant, I'm not sure how this compiles.
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.
Oops, mis-copy-n-pasted. Fixed.
|
I would get rid of |
|
Alternatively, if there was a way to have a tuple of |
| } | ||
| return Impl(key, value); | ||
| } | ||
| alias asTuple 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.
Yes, this is ugly. But it won't be when we can return tuples from functions (I hope?)
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 found a better way to do this. Instead of using key and value as field names, use the tuple as the field, and opDispatch the key and value.
Note that this means the tuple must be a tuple of pointers. An example I whipped up (updated):
struct S(T...)
{
T expand;
ref opDispatch(string s)() if(s == "key") {return *expand[0];}
ref opDispatch(string s)() if(s == "value") {return *expand[1];}
alias expand this;
}
void main()
{
int a;
string b;
S!(int*, string*) s;
s[0] = &a;
s[1] = &b;
a = 1;
assert(s.key == 1);
s.key = 2;
assert(a == 2);
b = "hi";
assert(s.value == "hi");
s.value = "there";
assert(b == "there");
}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 is much better, yes. However, it fails the requirement that is(typeof(aa.byPair().front[0]) == Key) because the type will be Key* now. :-(
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 OK with that ;) I'm sure others aren't though.
This gave me a great idea, will post in NG. |
|
I am strictly in favor of returning struct instance with |
|
This is getting really annoying... I think we should just forget about tuple support in druntime, it's completely broken anyway, we should just rename this method |
|
As I have already mentioned in NG I have no idea where this push for tuples comes from and think it is a bad idea. |
|
I agree with @Dicebot. Let's move forward with the The idea that you can't range over an AA with both key and value when both are imminently accessible is ridiculous.
Sorry about the push for tuples, I didn't realize how messy it currently is, I thought it would be easier. |
|
Renamed to |
|
LGTM. Can you change commit comment to "Implement AA byKeyValue"? Also, do you want to write an update to the online docs (apologies if you have already, I don't get notified of those)? We should wait until @Dicebot and @MartinNowak examine before pulling. This is going to be a huge improvement, but obviously should be done with care. |
|
LGTM. Seems simple enough not to cause any worries. |
|
Reworded. I haven't written up the doc changes yet; will submit it later. Thanks for the reminder, though, I totally forgot we need to update the docs! |
|
Here's the doc pull: dlang/dlang.org#743 |
|
Auto-merge toggled on |
|
If I compile this: I get: And isn't this: Better than: |
Wow. The dangers of copy+paste. Wasn't someone working on generating |
|
Fixed: #1077 |
|
A problem with a constant key-value pair: test.d(4,18): Error: mutable method object.byKeyValue!(int[int], int, int).byKeyValue.Result.front.Pair.key is not callable using a const object |
accessors in |
|
Done: #1079 |
|
Was it impossible to make that compatible with the existing tuple expansion in foreach? foreach (k, v; aa.byKeyValue.filter!pred)
{} |
|
The consensus was not to invent a tuple that doesn't pass std.traits.isTuple. So it could be done, but probably not without issues. You can wrap the result in a |
|
@MartinNowak Try this instead: |
|
Is there a good reason why |
|
I just didn't think about it at the time. Please file an enhancement in bugzilla. |
|
Although on second thoughts, we might have trouble with |
|
Yeah, keeping track of length means saving an arbitrary integer to go along with the range to count down as you pop front. For the times it would be used, I think it's not worth the added footprint.
We don't need to worry about that, it's an invalid range at that point. |
Resurrected from #574.
The basic idea is to provide this functionality in druntime, and if people insist, add a wrapper to Phobos that returns
Tuples as people clamored for last time. Or just recommend users add this one-liner wrapper to their own code:then you'll a
Tuplerange out of it for no extra charge. :-)