-
-
Notifications
You must be signed in to change notification settings - Fork 756
implement std.traits.isInstanceOf for non-types #4739
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5620,6 +5620,13 @@ enum bool isMutable(T) = !is(T == const) && !is(T == immutable) && !is(T == inou | |
| * Returns true if T is an instance of the template S. | ||
| */ | ||
| enum bool isInstanceOf(alias S, T) = is(T == S!Args, Args...); | ||
| /// ditto | ||
| template isInstanceOf(alias S, alias T) | ||
| { | ||
| enum impl(alias T : S!Args, Args...) = true; | ||
| enum impl(alias T) = false; | ||
| enum isInstanceOf = impl!T; | ||
| } | ||
|
|
||
| /// | ||
| @safe unittest | ||
|
|
@@ -5628,12 +5635,28 @@ enum bool isInstanceOf(alias S, T) = is(T == S!Args, Args...); | |
| static struct Bar(T...) { } | ||
| static struct Doo(T) { } | ||
| static struct ABC(int x) { } | ||
| static void fun(T)() { } | ||
| template templ(T) { } | ||
|
|
||
| static assert(isInstanceOf!(Foo, Foo!int)); | ||
| static assert(!isInstanceOf!(Foo, Bar!int)); | ||
| static assert(!isInstanceOf!(Foo, int)); | ||
| static assert(isInstanceOf!(Doo, Doo!int)); | ||
| static assert(isInstanceOf!(ABC, ABC!1)); | ||
| static assert(!__traits(compiles, isInstanceOf!(Foo, Foo))); | ||
| static assert(!isInstanceOf!(Foo, Foo)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might need some attention. I don't see a reason why
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's because the original implementation did not handle things different from types as second parameter, and |
||
| static assert(isInstanceOf!(fun, fun!int)); | ||
| static assert(isInstanceOf!(templ, templ!int)); | ||
| } | ||
|
|
||
| @safe unittest | ||
| { | ||
| static void fun1(T)() { } | ||
| static void fun2(T)() { } | ||
| template templ1(T) { } | ||
| template templ2(T) { } | ||
|
|
||
| static assert(!isInstanceOf!(fun1, fun2!int)); | ||
| static assert(!isInstanceOf!(templ1, templ2!int)); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have written this as
But: https://issues.dlang.org/show_bug.cgi?id=16403
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noice