-
-
Notifications
You must be signed in to change notification settings - Fork 706
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
Tuple range interface #3198
Tuple range interface #3198
Conversation
1f1332b to
7a40ffa
Compare
| /+ Helper for partial instanciation +/ | ||
| template isBuildableFrom(U) | ||
| { | ||
| enum isBuildableFrom(T) = isBuildable!(T, U); | ||
| } | ||
|
|
||
| /+ Determine whether or not all given types in T are the same type +/ | ||
| template areAllSame(T...) |
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.
Do you think there's any value in also supporting Tuples whose types can all implicitly convert to some supertype?
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 thought about this, but ultimately, I think it's better without. The biggest issue is that the element type wouldn't be an lvalue like it is with the current implementation.
|
This is an interesting PR, but it suffers somewhat from the fact that the following code works: And there is also as an option, as you said, |
No. The real advantage is that the range it returns refers to the original elements by reference, allowing code like the examples. |
| /+ Determine whether or not all given types in T are the same type +/ | ||
| template areAllSame(T...) | ||
| { | ||
| static if (T.length == 0 || T.length == 1) |
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.
Otherwise known as T.length <= 1 ;)
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 this is more explicit, i.e. "match the nullary and unary cases".
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.
That's more likely to raise a brow with most maintainers than the simpler condition, so please change and let's merge this. I approve the PR and will archive this.
7a40ffa to
bf9c047
Compare
|
|
bf9c047 to
1cdca5e
Compare
|
This is fine. In the future we may expand functionality to the empty tuple and tuples that contain qualified types with different qualifiers etc. |
1cdca5e to
86de84c
Compare
|
Fixed the awkward condition and rebased. |
f411658 to
2283d5d
Compare
|
Amended it to handle tuples containing types of varying mutability. For nullary |
2283d5d to
d318811
Compare
d318811 to
6d89732
Compare
| } | ||
|
|
||
| /// | ||
| nothrow unittest |
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.
If you put the unittest inside the struct, there will be a new unittest instantiated each time Tuple is instantiated. Jonathan Davis pointed this out in the newsgroup and I think it's something we should avoid.
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.
Yeah, but it should be done for the whole type. I pointed out the same in #3594.
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.
Ndslice does this a lot, too.
AFAIK this works as long as the are enough outside tests covering all types.
See #4050
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 a performance issue: code is generated and executed an unnecessary amount of times. I haven't heard of any measurements indicating that Tuple in particular is a problem but it's good to avoid in principle.
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 adding version (unittest){}?
|
I have suffered from the bad interoperability between tuples and arrays a lot since I started with D and I would like to see this coming too :) If I understood this PR, this we can do this in both ways? Would be pretty useful! |
|
Neither of the two operations you posted would work with this. There is a distinction between arrays and ranges, and this PR adds a range interface, not an array interface, to tuples. Here's how you do those operations in range style: Tuple!(int, int) tup = tuple(1, 2);
int[] arr1 = tup.range.array; // Assuming you meant making a copy, as otherwise you'd just compare the slice with itself
assert(tup.range == arr1);
// and back again
int[] arr2 = [3, 4];
arr2.copy(tuple.range);
assert(tup.range == arr2); |
What do you think about adding this example as another visible unittest in the documentation? |
Fix ticket 5489 and 9871
6d89732 to
2697b9e
Compare
|
@JakobOvrum now autotester fails ... |
OMG looks like compiler internal error, all more valuable to reduce. |
Then let's bug it? Do you get from the error message what exactly fails? Is someone here using FreeBSD? |
|
No activity from the author in nine months. Closing. |
Adds a range interface for tuples with homogeneous types. The introduced range interface presents the fields by reference, so it's very different from
only(myTuple.expand), as the examples show.The whitespace fixes are in a separate commit, so the diff for the second commit is probably easier to look at than the total diff.
Could the new
returnattribute help with making this a memory safe interface?Do tuples with internal padding exist? Or rather, do template argument lists with padding exist? I wasn't able to produce any with DMD32/Windows/OMF; I do have the code that handles it properly so it's readily available if padding is possible. Right now this patch includes a static assert checking that no tuple with padding is ever instantiated.
Apparently, documented unittests inside a documented static-if like this confuses DDoc quite severely, so the DDoc description is duplicated three times in the output. I'll see if the bug tracker has anything on this.