Skip to content

Commit

Permalink
sumtype: work around issue 21975 in isSumType
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pbackus authored and dlang-bot committed Feb 27, 2022
1 parent 6bf4314 commit 2de6834
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion std/sumtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,27 @@ private enum bool isSumTypeInstance(T) = is(T == SumType!Args, Args...);
}

/// True if `T` is a [SumType] or implicitly converts to one, otherwise false.
enum bool isSumType(T) = is(T : SumType!Args, Args...);
template isSumType(T)
{
static if (is(T : SumType!Args, Args...))
{
enum isSumType = true;
}
else static if (is(T == struct) && __traits(getAliasThis, T).length > 0)
{
// Workaround for https://issues.dlang.org/show_bug.cgi?id=21975
import std.traits : ReturnType;

alias AliasThisType = ReturnType!((T t) =>
__traits(getMember, t, __traits(getAliasThis, T)[0])
);
enum isSumType = .isSumType!AliasThisType;
}
else
{
enum isSumType = false;
}
}

///
@safe unittest
Expand All @@ -1590,6 +1610,25 @@ enum bool isSumType(T) = is(T : SumType!Args, Args...);
assert(!isSumType!ContainsSumType);
}

@safe unittest
{
static struct AliasThisVar(T)
{
SumType!T payload;
alias payload this;
}

static struct AliasThisFunc(T)
{
SumType!T payload;
ref get() { return payload; }
alias get this;
}

static assert(isSumType!(AliasThisVar!int));
static assert(isSumType!(AliasThisFunc!int));
}

/**
* Calls a type-appropriate function with the value held in a [SumType].
*
Expand Down

0 comments on commit 2de6834

Please sign in to comment.