Why should this compile? What is the compelling reason?
Comment #3 by k.hara.pg — 2012-06-13T23:05:57Z
(In reply to comment #2)
> Why should this compile? What is the compelling reason?
typeof(null) is a subtype of array/class/pointer types. By return type covariance rule, B.foo should be able to override A.foo.
Comment #4 by bugzilla — 2012-06-14T00:39:47Z
(In reply to comment #3)
> typeof(null) is a subtype of array/class/pointer types. By return type
> covariance rule, B.foo should be able to override A.foo.
Since A is not a base class of null, I do not think this is valid. foo could return B, and that will work, because A is a base class of B.
Comment #5 by k.hara.pg — 2012-06-14T01:13:26Z
(In reply to comment #4)
> (In reply to comment #3)
> > typeof(null) is a subtype of array/class/pointer types. By return type
> > covariance rule, B.foo should be able to override A.foo.
>
> Since A is not a base class of null, I do not think this is valid. foo could
> return B, and that will work, because A is a base class of B.
No. Indeed A is not a base class of null _liteal_, but A is a base _type_ of typeof(null).
Current dmd compiles following tests:
static assert(is(typeof(null) : int[])); // int[] is a base type of typeof(null)
static assert(is(typeof(null) : Object)); // Object is a base type of typeof(null)
static assert(is(typeof(null) : int*)); // int* is a base type of typeof(null)
I have implemented typeof(null) as like Scala's Nothing type, so this is expected result.
In other words, typeof(null) is the most derived class type from all of class types. So dmd should compile the original code.
Comment #6 by github-bugzilla — 2012-06-22T21:31:29Z