D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20178 - Add TypeInfo_Class/TypeInfo_Interface.isBaseOf (equivalent to C#/Java isAssignableFrom)
Summary: Add TypeInfo_Class/TypeInfo_Interface.isBaseOf (equivalent to C#/Java isAssig...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2019-08-28 23:49 UTC by Nathan S.
Modified: 2020-04-16 19:31 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Nathan S. 2019-08-28 23:49:45 UTC
C# System.Type and Java java.lang.Class have `isAssignableFrom` to conveniently determine at runtime whether a class inherits from or implements another class or interface. Adding something similar to TypeInfo_Class and TypeInfo_Interface would be useful.

The name "isAssignableFrom" would be familiar to people coming from C# or Java but is potentially misleading since since -- due to "alias this" and the ability to overload opAssign -- whether a value of type Y can be assigned to a variable of type X is not the same as asking if Y inherits from or implements X. The language "derives from" is used in the D specification so I'm suggesting "isDerivedFrom" swapping the receiver and argument relative to "isAssignableFrom."
Comment 1 Dlang Bot 2019-08-29 00:09:32 UTC
@n8sh created dlang/druntime pull request #2770 "Give TypeInfo_Class/TypeInfo_Interface.isDerivedFrom like C#/Java isAssignableFrom" fixing this issue:

- 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.

https://github.com/dlang/druntime/pull/2770
Comment 2 Dlang Bot 2020-04-16 19:31:06 UTC
dlang/druntime pull request #2770 "Give TypeInfo_Class/TypeInfo_Interface.isBaseOf like C#/Java isAssignableFrom" was merged into master:

- bc10972b8e5d6c70a9fce3abcc598f6cf120f08d by Nathan Sashihara:
  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.

https://github.com/dlang/druntime/pull/2770