-
-
Notifications
You must be signed in to change notification settings - Fork 749
Fix Issue 21731 - SumType should provide convenient access to the typ… #7886
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
Conversation
|
Thanks for your pull request and interest in making D better, @pbackus! 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 references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#7886" |
| { | ||
| alias MySum = SumType!(const(int[]), int[]); | ||
|
|
||
| int[] ma = [1, 2, 3]; |
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.
| int[] ma = [1, 2, 3]; | |
| int[3] ma = [1, 2, 3]; |
Wouldn't that be enough to make it work in betterC ?
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.
In theory, yes. In practice, if I use a stack-allocated static array and construct the SumType with a slice (i.e., MySum(ma[])), the tests will fail even with -preview=dip1000, because the workaround for issue 21229 used in SumType's constructors does not correctly preserve lifetime information about the constructor's argument.
|
Why isn't this just |
|
In some rare cases (see unittests) it is possible for the same type to have two different tag values. The current implementation always returns a unique value for a given type, but conversation at pbackus/sumtype#57 has convinced me that this may not be desirable. |
|
Hm... so in a case where you have SumType!(int, const int), assigned to a const int, the tag will be 1 with a standard instance, and 0 with a const instance? I'll point out that returning a value of 1 is still going to be valid in the list of (const int, const int). I would recommend just returning the tag, it's the most expected result, even on a const SumType. |
|
Yes, I've come the same conclusion myself. |
af1f560 to
5ed6ffd
Compare
|
Buildkite failure appears to be spurious: |
5ed6ffd to
50c7945
Compare
schveiguy
left a comment
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.
Looks good. Trivial change, looks like some docs were left over from the previous incarnation.
std/sumtype.d
Outdated
| * `SumType`'s `Types`. | ||
| * | ||
| * If the `SumType` is qualified, then its qualifiers are applied to | ||
| * `Types` before determining the index. |
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 second paragraph should be removed.
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.
Should it? In the case of something like const(SumType!(A, B))(const(A)()), the type of the value is const(A), which is not present in Types. The first paragraph alone is insufficient to determine the correct behavior in this case (although I'll grant that it is not difficult to guess).
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.
Well, you aren't applying the qualifier. You are just returning the tag.
The paragraph suggests something different happens when a qualifier is used.
Perhaps a rewrite is needed. Maybe:
If the SumType is qualified, the index indicates the Type if it were unqualified.
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.
The specific wording you suggest is incorrect, because the type in Types might already be the qualified version, but I agree that a rewrite is the best solution here.
The new wording describes the result without implying anything about the implementation.
…e index
See pbackus/sumtype#57.