Created
March 13, 2020 11:53
-
-
Save FeepingCreature/61d1ead8b744cfa2152849e5b6680bb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module unqualify; | |
import std.traits : isFunction, Unqual; | |
/// unqual that detects and works around https://issues.dlang.org/show_bug.cgi?id=20670 | |
template unqualify(T) | |
{ | |
static if (is(T == struct)) | |
{ | |
static if (T.tupleof.length > 0) | |
{ | |
// member->parent path. reliable, but only works if the struct has any members. | |
alias unqualify = __traits(parent, T.tupleof[0]); | |
} | |
else | |
{ | |
static if (!isFunction!(__traits(parent, T)) && !__traits(isTemplate, __traits(parent, T))) | |
{ | |
// parent->member path. only works if the parent of T is an aggregate. | |
alias unqualify = __traits(getMember, __traits(parent, T), __traits(identifier, T)); | |
} | |
else | |
{ | |
// we have no way to get at the 'true' T | |
// give up, fall back to Unqual, and hope it's not immutable struct | |
alias unqualify = Unqual!T; | |
} | |
} | |
} | |
else | |
{ | |
alias unqualify = Unqual!T; | |
} | |
} | |
struct S { } | |
immutable struct T { } | |
@("unqualify with empty structs") | |
unittest | |
{ | |
alias _ = unqualify!(S); // test compilation | |
static assert(is(unqualify!(S) == S)); | |
static assert(is(unqualify!(immutable S) == S)); | |
static assert(is(unqualify!(T) == T)); | |
static assert(is(unqualify!(immutable T) == T)); | |
} | |
@("unqualify with structs with member") | |
unittest | |
{ | |
alias _ = unqualify!(S); // test compilation | |
struct S { int i; } | |
immutable struct T { int k; } | |
static assert(is(unqualify!(S) == S)); | |
static assert(is(unqualify!(immutable S) == S)); | |
static assert(is(unqualify!(T) == T)); | |
static assert(is(unqualify!(immutable T) == T)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment