Skip to content
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

Procedural API for SumType #10650

Merged
merged 8 commits into from
Mar 4, 2025
Merged

Procedural API for SumType #10650

merged 8 commits into from
Mar 4, 2025

Conversation

pbackus
Copy link
Contributor

@pbackus pbackus commented Feb 24, 2025

This PR introduces three new convenience functions that allow code using SumType to be written in a more procedural style (as opposed to the functional style of match).

  • has!T checks whether a SumType contains a value of type T.
  • get!T accesses a SumType's value if it has type T, or asserts if it does not.
  • tryGet!T is like get!T, but throws an exception instead of asserting on failure.

To make room for get!T, the existing SumType.get method is renamed to getByIndex.

CC @atilaneves since this introduces new public symbols
CC @jmdavis who requested this in the community Discord

@dlang-bot
Copy link
Contributor

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 verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

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

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#10650"

@LightBender
Copy link
Contributor

LightBender commented Feb 25, 2025

Does this need a changelog entry or a corresponding GH issue? Beyond that I approve of this.

@pbackus
Copy link
Contributor Author

pbackus commented Feb 25, 2025

Added a changelog entry.

@pbackus
Copy link
Contributor Author

pbackus commented Feb 27, 2025

CI failure appears to be a bug in the ImportC test suite:

src\importc_compare.d-mixin-134(134): Error: C preprocess command C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\bin\HostX64\x64\cl.exe failed for file src\importc_includes.c, exit status 2

[...]

C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\vcruntime_c11_stdatomic.h(16): fatal error C1189: #error: "C atomics require C11 or later"

@LightBender LightBender added the Merge:72h no objection -> merge The PR will be merged if there are no objections raised. label Feb 28, 2025
@pbackus
Copy link
Contributor Author

pbackus commented Mar 4, 2025

This has been sitting with the 72h no objection -> merge label for 4 days, so I'm clicking merge.

@pbackus pbackus merged commit 3f990a7 into dlang:master Mar 4, 2025
10 checks passed
pbackus added a commit to pbackus/sumtype that referenced this pull request Mar 4, 2025
This change introduces three new convenience functions that allow code
using SumType to be written in a more procedural style (as opposed to
the functional style of match).

* has!T checks whether a SumType contains a value of type T.
* get!T accesses a SumType's value if it has type T, or asserts if it
  does not.
* tryGet!T is like get!T, but throws an exception instead of asserting
  on failure.

To make room for get!T, the existing SumType.get method is renamed to
getByIndex.

Phobos PR: dlang/phobos#10650
pbackus added a commit to pbackus/sumtype that referenced this pull request Mar 4, 2025
This change introduces three new convenience functions that allow code
using SumType to be written in a more procedural style (as opposed to
the functional style of match).

* has!T checks whether a SumType contains a value of type T.
* get!T accesses a SumType's value if it has type T, or asserts if it
  does not.
* tryGet!T is like get!T, but throws an exception instead of asserting
  on failure.

To make room for get!T, the existing SumType.get method is renamed to
getByIndex.

Frontend requirement is bumped to 2.103 for __traits(fullyQualifiedName).

Phobos PR: dlang/phobos#10650
@pbackus pbackus deleted the sumtype-proc-api branch March 11, 2025 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merge:72h no objection -> merge The PR will be merged if there are no objections raised.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants