Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions std/sumtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,18 @@ public:
return this.match!hashOf;
}
}

/**
* Returns the index of the type of the `SumType`'s current value in the
* `SumType`'s `Types`.
*
* If the `SumType` is qualified, returns the index of the type in `Types`
* whose qualified version matches the `SumType`'s current value.
*/
size_t typeIndex() const
{
return tag;
}
}

// Construction
Expand Down Expand Up @@ -1466,6 +1478,67 @@ version (D_BetterC) {} else
MySum y; y = b;
}

// Type index
@safe unittest
{
alias MySum = SumType!(int, float);

static bool isIndexOf(Target, Types...)(size_t i)
{
switch (i)
{
static foreach (tid, T; Types)
case tid: return is(T == Target);
default: return false;
}
}

assert(isIndexOf!(int, MySum.Types)(MySum(42).typeIndex));
assert(isIndexOf!(float, MySum.Types)(MySum(3.14).typeIndex));
}

// Type index for qualified SumTypes
// Disabled in BetterC due to use of dynamic arrays
version (D_BetterC) {} else
@safe unittest
{
alias MySum = SumType!(const(int[]), int[]);

static bool isIndexOf(Target, Types...)(size_t i)
{
switch (i)
{
static foreach (tid, T; Types)
case tid: return is(T == Target);
default: return false;
}
}

int[] ma = [1, 2, 3];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int[] ma = [1, 2, 3];
int[3] ma = [1, 2, 3];

Wouldn't that be enough to make it work in betterC ?

Copy link
Contributor Author

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.

// Construct as mutable and convert to const to get mismatched type + tag
auto x = MySum(ma);
const y = MySum(ma);
auto z = const(MySum)(ma);

assert(isIndexOf!(int[], MySum.Types)(x.typeIndex));
assert(isIndexOf!(const(int[]), Map!(ConstOf, MySum.Types))(y.typeIndex));
assert(isIndexOf!(const(int[]), Map!(ConstOf, MySum.Types))(z.typeIndex));
}

// Type index for differently-qualified versions of the same SumType
// Disabled in BetterC due to use of dynamic arrays
version (D_BetterC) {} else
@safe unittest
{
alias MySum = SumType!(const(int[]), int[]);

int[] ma = [1, 2, 3];
auto x = MySum(ma);
const y = x;

assert(x.typeIndex == y.typeIndex);
}

/// True if `T` is an instance of the `SumType` template, otherwise false.
private enum bool isSumTypeInstance(T) = is(T == SumType!Args, Args...);

Expand Down