Bug 11482 – Add `alias this` support for `null` class instances

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-09T05:55:04Z
Last change time
2018-10-17T11:32:15Z
Assigned to
No Owner
Creator
Denis Shelomovskii

Comments

Comment #0 by verylonglogin.reg — 2013-11-09T05:55:04Z
As `alias this` may be used to inheritance support a conversion from base to derived is expected to work also for `null` class instances. A possible solution as to allow aliasing static or non-member function which accepts the type as a parameter: --- class A { } A asA(B b) { return b ? b.a : null; } class B { A a; alias asA this; } void main() { B b = null; A a = b; // rewritten as `asA(b)` } --- Based on Issue 2929.
Comment #1 by razvan.nitu1305 — 2018-10-17T11:32:15Z
Why would you even do that? Why not make B inherit A and then you have no problem. Overcomplicating alias this will lead to even more problems when composed with other language features. Closing this as invalid.