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.isBaseOf
Equivalent to C#/Java isAssignableFrom. 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. Adding qualifiers to rt.cast_ functions.
- Loading branch information
Showing
5 changed files
with
134 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Added TypeInfo_Class/TypeInfo_Interface.isBaseOf that works like C#/Java isAssignableFrom. | ||
|
||
`TypeInfo_Class.isBaseOf` returns true if the argument and the receiver | ||
are equal or if the class represented by the argument inherits from the | ||
class represented by the receiver. This is called `isBaseOf` instead of | ||
`isAssignableFrom` to avoid confusion for classes that overload | ||
`opAssign` and so may allow assignment from classes outside their | ||
inheritance hierarchy and to match existing terminology in the D | ||
runtime. `TypeInfo_Interface.isBaseOf` is similar with the addition | ||
that the argument may be either `TypeInfo_Class` or | ||
`TypeInfo_Interface`. | ||
------- | ||
class ClassA {} | ||
class ClassB : ClassA {} | ||
|
||
auto a = new ClassA(), b = new ClassB(); | ||
|
||
assert(typeid(a).isBaseOf(typeid(a))); | ||
assert(typeid(a).isBaseOf(typeid(b))); | ||
assert(!typeid(b).isBaseOf(typeid(a))); | ||
------- |
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
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,43 @@ | ||
// https://issues.dlang.org/show_bug.cgi?id=20178 | ||
|
||
interface I {} | ||
interface J : I {} | ||
class C1 : I {} | ||
class C2 : C1 {} | ||
class C3 : J {} | ||
|
||
void main() @nogc nothrow pure @safe | ||
{ | ||
assert(typeid(C1).isBaseOf(typeid(C1))); | ||
assert(typeid(C1).isBaseOf(typeid(C2))); | ||
|
||
assert(!typeid(C2).isBaseOf(typeid(C1))); | ||
assert(typeid(C2).isBaseOf(typeid(C2))); | ||
|
||
assert(!typeid(C1).isBaseOf(typeid(Object))); | ||
assert(!typeid(C2).isBaseOf(typeid(Object))); | ||
assert(typeid(Object).isBaseOf(typeid(C1))); | ||
assert(typeid(Object).isBaseOf(typeid(C2))); | ||
|
||
assert(typeid(I).isBaseOf(typeid(I))); | ||
assert(typeid(I).isBaseOf(typeid(J))); | ||
assert(typeid(I).isBaseOf(typeid(C1))); | ||
assert(typeid(I).isBaseOf(typeid(C2))); | ||
assert(typeid(I).isBaseOf(typeid(C3))); | ||
assert(!typeid(I).isBaseOf(typeid(Object))); | ||
|
||
assert(!typeid(J).isBaseOf(typeid(I))); | ||
assert(typeid(J).isBaseOf(typeid(J))); | ||
assert(!typeid(J).isBaseOf(typeid(C1))); | ||
assert(!typeid(J).isBaseOf(typeid(C2))); | ||
assert(typeid(J).isBaseOf(typeid(C3))); | ||
assert(!typeid(J).isBaseOf(typeid(Object))); | ||
|
||
// Because isBaseOf is final it currently doesn't automatically | ||
// segfault when the receiver is null. Verify it doesn't return | ||
// true when the receiver and argument are both null. (Segfaulting | ||
// would also be an acceptable behavior.) | ||
assert(!(cast(TypeInfo_Class) null).isBaseOf(null)); | ||
assert(!(cast(TypeInfo_Interface) null).isBaseOf(cast(TypeInfo_Interface) null)); | ||
assert(!(cast(TypeInfo_Interface) null).isBaseOf(cast(TypeInfo_Class) null)); | ||
} |