This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Issue 20178 - Add TypeInfo_Class/TypeInfo_Interface.isDerivedFrom
Equivalent to C#/Java isAssignableFrom with the argument swapped with the receiver. Naming the method "isAssignableFrom" would be more familiar to people coming from C#/Java but is potentially misleading: "alias this" and overloadable opAssign mean that this would not actually indicate whether values of one type could be assigned to another.
- Loading branch information
Showing
3 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// https://issues.dlang.org/show_bug.cgi?id=20178 | ||
|
||
interface I {} | ||
interface J : I {} | ||
class C1 : I {} | ||
class C2 : C1 {} | ||
class C3 : J {} | ||
|
||
// It might seem sensible to give TypeInfo_Class & TypeInfo_Interface | ||
// a method called "isAssignableFrom" instead of "isDerivedFrom" since | ||
// that matches C# and Java. However, the existences of "alias this" and | ||
// overloadable "opAssign" mean that that this would not actually indicate | ||
// whether arguments of the argument type could be assigned to a field | ||
// whose value is the receiver's type. | ||
bool isAssignableFrom(T,U)(T parent, U child) | ||
{ | ||
return child.isDerivedFrom(parent); | ||
} | ||
|
||
void main() @nogc nothrow pure @safe | ||
{ | ||
assert(typeid(C1).isAssignableFrom(typeid(C1))); | ||
assert(typeid(C1).isAssignableFrom(typeid(C2))); | ||
|
||
assert(!typeid(C2).isAssignableFrom(typeid(C1))); | ||
assert(typeid(C2).isAssignableFrom(typeid(C2))); | ||
|
||
assert(!typeid(C1).isAssignableFrom(typeid(Object))); | ||
assert(!typeid(C2).isAssignableFrom(typeid(Object))); | ||
assert(typeid(Object).isAssignableFrom(typeid(C1))); | ||
assert(typeid(Object).isAssignableFrom(typeid(C2))); | ||
|
||
assert(typeid(I).isAssignableFrom(typeid(I))); | ||
assert(typeid(I).isAssignableFrom(typeid(J))); | ||
assert(typeid(I).isAssignableFrom(typeid(C1))); | ||
assert(typeid(I).isAssignableFrom(typeid(C2))); | ||
assert(typeid(I).isAssignableFrom(typeid(C3))); | ||
assert(!typeid(I).isAssignableFrom(typeid(Object))); | ||
|
||
assert(!typeid(J).isAssignableFrom(typeid(I))); | ||
assert(typeid(J).isAssignableFrom(typeid(J))); | ||
assert(!typeid(J).isAssignableFrom(typeid(C1))); | ||
assert(!typeid(J).isAssignableFrom(typeid(C2))); | ||
assert(typeid(J).isAssignableFrom(typeid(C3))); | ||
assert(!typeid(J).isAssignableFrom(typeid(Object))); | ||
} |