Commit graph

49 commits

Author SHA1 Message Date
Paul Backus
613ec088e4 sumtype: rename 'degrees' to 'value' in example
Strictly speaking, the Kelvin scale is not measured in degrees, so the
previous name was incorrect. Changing it in all cases preserves
consistency.

Fixes #10540
2025-03-11 10:33:32 -04:00
Paul Backus
1d3177ff2a sumtype: prevent ambiguity in failedGetMessage 2025-02-27 10:01:43 -05:00
Paul Backus
03f7dbab9d sumtype: make has!T.checkType helper private 2025-02-27 09:49:29 -05:00
Paul Backus
7e34f06d83 sumtype: extract common code from get and tryGet 2025-02-24 14:26:39 -05:00
Paul Backus
688ecf2089 sumtype: add tryGet!T, a throwing version of get!T 2025-02-24 14:25:51 -05:00
Paul Backus
756e50dde1 sumtype: add get!T to access a SumType's value 2025-02-24 14:24:47 -05:00
Paul Backus
391ff50015 sumtype: add has!T to check type of SumType value 2025-02-24 14:23:11 -05:00
Paul Backus
fe64ac592b Rename SumType.get to getByIndex 2025-02-23 20:32:07 -05:00
Paul Backus
40daf61851
sumtype: fix canMatch for non-copyable ref return (#10648)
Handlers that return a non-copyable value by reference are now capable
of successfully matching.

Fixes #10647
2025-02-24 06:12:00 +08:00
Inkrementator
f52a44d401
Fix Bugzilla Issue 24818 Sumtype with single-type wastes space (#10642)
* Fix Bugzilla Issue 24818 Sumtype with single-type wastes space

* Move unittest to correct position

* Annotate unittest with @safe
2025-02-18 19:49:59 -08:00
Paul Backus
c5d074b12e Refactor: access SumType value by index, not type
Previously, code that already knew the desired index was forced to
compute a type to pass to get!T or memberName!T, just for get!T or
memberName!T to turn that type back into an index. Removing this
unnecessary round trip simplifies the code.

Additionally, since memberName is no longer dependent on SumType.Types,
it can be moved to module scope, and its instantiations can be shared
across different SumType instances.
2025-02-03 00:46:36 +01:00
Paul Backus
f3d92d9f9e sumtype: add fast path for single dispatch
This reduces the number of templates instantiated by matchImpl in the
common case where only one SumType is being matched on.
2024-11-20 15:25:01 -05:00
Paul Backus
be59d94737 sumtype: avoid template recursion in handlerArgs 2024-11-20 15:25:01 -05:00
Paul Backus
7362ac5bee sumtype: reduce instantiations of handlerArgs
Calls to matchImpl whose SumType arguments have the same dimensions will
now share instantiations, instead of having separate ones.
2024-11-20 15:24:43 -05:00
Paul Backus
b883c5ce92 sumtype: reduce instantiations of TagTuple
Tuples with the same dimensions now share a single instantiation,
regardless of the types held by the SumTypes they are used for.
2024-11-20 14:51:17 -05:00
Paul Backus
4c41b433dc sumtype: put private matchImpl helpers together 2024-11-20 14:41:44 -05:00
Paul Backus
ed91e50deb sumtype: add list of examples to module doc
This will hopefully make it easier for readers to notice that all of
these examples exist.
2024-05-31 11:22:43 -04:00
Paul Backus
1aa99f8e3f sumtype: rename arithmetic evaluator example 2024-05-31 11:22:43 -04:00
Paul Backus
d1dfd094f4 sumtype: add overload-set matching example
This is a useful technique, and non-obvious enough that other D
community members were surprised when I showed it to them.

The unusual formatting and code layout used here achieves the following
goals:

1. Have this example appear below "Basic usage".
2. Have the overload set appear as it would at module level.
3. Have as much of the example code unit-tested as possible.

Goal (2), in particular, rules out the use of a "wrapper" struct to
create an overload set of static methods, which is the technique that's
normally used to include an overload set in a unittest block.
2024-05-31 11:22:43 -04:00
Paul Backus
e13bd7dec6 sumtype: remove match-by-introspection example
This technique has turned out not to be very useful in practice, and the
example is a bit subtle and tricky to understand. Removing it will make
room for more useful and less confusing examples.
2024-05-31 00:08:44 -04:00
Paul Backus
bfe2a18389 sumtype: add hint to "never matches" message
A common cause of the "handler never matches" error is a template
handler that contains a typo or other compile-time error. Since the
location of the actual error is suppressed by __traits(compiles), users
often do not think to look for a mistake inside the handler itself. Now,
the message itself includes a hint to do so.
2024-03-03 05:37:21 +01:00
Lucian Danescu
8293310cd8 rename variables 2022-11-09 23:28:55 +02:00
Lucian Danescu
452b1def78 same name fix 2022-11-07 19:20:19 +02:00
Paul Backus
e26aaeb767 Remove SumType's invariant
According to earlier versions of the language spec, checking the
invariant of a struct would also cause its fields' invariants to be
checked, recursively. SumType's invariant was added to make SumType
behave consistently with other structs in this regard.

The spec, however, was wrong: invariants of struct fields are not
checked unless the field is accessed directly, and never have been.
Thus, to make SumType behave consistently with other structs, its
invariant must be removed.

This change should not break any valid programs, since code that relies
on the failure of an invariant has undefined behavior per the spec.

Spec correction PR: https://github.com/dlang/dlang.org/pull/3405
2022-09-04 23:40:03 -04:00
Geod24
87c6e7e354 std.sumtype: Move TagTuple to module level to reduce instantiations
TagTuple wass previously nested within matchImpl, meaning it gets
a different instantiation for each match call with different handlers.
This is not required, as the only template parameters it needs are
the SumTypes being used. In practice, we might even get away with
using the length, but the gain is likely to be marginal compared
to the gains from moving it out of matchImpl.
Another underlying motivation for this change is that it works around
a compiler bug triggered with complex code using const SumType
(the compiler complains that 'this' for 'invariant' is of the wrong type,
const vs non-const).
2022-07-07 00:05:35 +00:00
Paul Backus
a504a5e7d6 Fix Issue 23182 - Can't assign struct with opAssign to SumType in CTFE
SumType.opAssign now avoids calling core.lifetime.move or
core.lifetime.forward during CTFE whenever possible.
2022-06-14 04:38:57 +00:00
João Lourenço
5e746a0490
test(sumtype): utilize D_Invariants conditional compilation
Signed-off-by: João Lourenço <jlourenco5691@gmail.com>
2022-05-30 10:35:07 +01:00
João Lourenço
d39c4ea290
test(sumtype): add unittests when returning the matched value's reference
Signed-off-by: João Lourenço <jlourenco5691@gmail.com>
2022-05-11 21:23:57 +01:00
João Lourenço
6069c44930
fix(sumtype): template canMatch does not account for ref when matching
The template `canMatch` does not account for `ref`.
The template `valueTypes` stores all types of the member values and uses SumTypes's `get` function, which returns a `ref`.
However, ref does not persist and the type is not sent to `canMatch` as a `ref`.
Because of this, when matching, `canMatch` will fail as it will test for a copy, and returning a reference of that value results in escaping it.

Fix Issue 23101

Signed-off-by: João Lourenço <jlourenco5691@gmail.com>
2022-05-10 20:25:46 +01:00
Paul Backus
c6b4bd9638 [Refactor] Move toCtString to std.conv
Previously, it was duplicated in std.functional and std.sumtype.
2022-03-26 14:04:37 +00:00
Paul Backus
d9d104c9bc Fix issue 22901 - Can't construct inout SumType
The template constraint is needed to ensure that the inout constructor
overload is only considered when it is an exact match, without qualifier
conversions.

Without the constraint, a constructor call such as

    const(SumType!(int[]))([1, 2, 3])

...would be ambiguous, since it would match both the const constructor
overload and the inout constructor overload at the "match with qualifier
conversion" level.
2022-03-20 14:57:40 -04:00
canopyofstars
668d43c5df Fix 22851 - Add source reference to std.sumtype
Add `Source: $(PHOBOSSRC std/sumtype.d)` to `std.sumtype's` module documentation.
2022-03-06 03:34:35 +00:00
Paul Backus
a1f8c4c070 Revert "sumtype: work around issue 21975 in isSumType"
This reverts commit 2de68340ae.
2022-03-01 15:09:30 +00:00
Paul Backus
2de68340ae sumtype: work around issue 21975 in isSumType
isSumType!T now evaluates to true instead of false when T is a templated
struct type that implicitly converts to a SumType via alias this.
2022-02-27 23:15:28 +00:00
Paul Backus
5e3c4af308 sumtype: add example of memory corruption to docs
This makes it clearer why SumType.opAssign must sometimes be @system.
2022-02-23 18:30:07 +00:00
Paul Backus
5ca44b74a5 Fix Issue 22572 - Cannot define SumType over immutable struct with Nullable
Previously, SumType incorrectly assumed that all members of an
inout(SumType) must themselves be inout-qualified. However, this is not
the case when one of those member types is declared as immutable, since
the qualifier combination `immutable inout` collapses to just
`immutable`.

Attempting to copy an immutable member type as though it were inout
caused matching to fail in SumType's copy constructor, due to the
following (gagged) error:

  Error: `inout` on `return` means `inout` must be on a parameter as
  well for `pure nothrow @nogc @safe inout(Storage)(ref immutable(Value)
  value)`
2022-02-09 22:50:10 +00:00
wolframw
cab079a276 sumtype: remove redundant "See Also" in tryMatch documentation 2021-12-22 21:24:30 +00:00
Paul Backus
5ac515b52c Fix Issue 22117 - Can't store scope pointer in a SumType
Previously, the assignment of the local variable 'newStorage' to the
longer-lived member variable 'storage' caused scope inference to
(correctly) fail.

newStorage was necessary to work around issues 21229 and 22118. Since
those issues have been fixed, newStorage can be safely removed.
2021-08-23 21:52:40 -04:00
Paul Backus
f96f8046d9 Fix Issue 22225 - SumType: Some assignments should be able to execute in safe code 2021-08-19 23:07:12 +00:00
Paul Backus
32f32525bf Fix issue 22077 - std.sumtype support for copy constructors is incomplete
Previously, SumType would define the wrong set of constructors for types
whose copyability varies depending on their mutability--a situation that
was impossible with postblits, but is now possible with copy
constructors.
2021-08-12 03:23:11 +00:00
Dennis
1cb398ffcd
Disable non-@nogc unittest in -betterC (#8163) 2021-07-13 08:55:35 +08:00
berni44
b2019ebab0 Narrow imports of std.math in the rest of phobos. 2021-04-21 03:00:57 +02:00
Atila Neves
7407ce93cd Revert "Fix Issue 21731 - SumType should provide convenient access to the typ…" 2021-03-26 22:01:23 +01:00
The Dlang Bot
5dd4446c80
Merge pull request #7886 from pbackus/sumtype-type-index
Fix Issue 21731 - SumType should provide convenient access to the typ…
merged-on-behalf-of: Steven Schveighoffer <schveiguy@users.noreply.github.com>
2021-03-20 15:10:59 +01:00
Paul Backus
e1baa9b6a0 Reword documentation for SumType.typeIndex
The new wording describes the result without implying anything about the
implementation.
2021-03-20 00:19:28 -04:00
Paul Backus
50c7945236 Fix Issue 21731 - SumType should provide convenient access to the type index 2021-03-19 17:12:27 -04:00
berni44
6f2a0934a7 Adapt imports of std.format to new structure of std.format. 2021-03-19 13:22:00 +01:00
Paul Backus
56bc37c8e5 Fix Issue 21708 - SumType.opEquals gives confusing error message 2021-03-13 23:28:48 +01:00
Paul Backus
51a70ee267
Add sumtype to Phobos (#7702)
Add sumtype to Phobos
merged-on-behalf-of: unknown
2021-03-05 12:41:34 +01:00