Bug 12570 – cast to template interface

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2014-04-12T23:34:00Z
Last change time
2014-04-14T03:43:16Z
Assigned to
nobody
Creator
sigod.mail

Comments

Comment #0 by sigod.mail — 2014-04-12T23:34:53Z
Consider this code: //////////////////////////////// import std.stdio; interface A {} interface B(T : A) : A {} class C_a : A {} class B_a : B!C_a {} void main() { B_a b = new B_a; B!A b_a = b; writeln(b_a); } //////////////////////////////// Compiler says: Error: cannot implicitly convert expression (b) of type B_a to B!(A).B Shouldn't it be allowed? However, explicit cast result in null.
Comment #1 by dlang-bugzilla — 2014-04-14T03:43:16Z
No, it should not be allowed. Consider the following extension of your example: //////////////////////////////// import std.stdio; interface A {} interface B(T : A) : A { // Add a method that accepts a T value. void foo(T c); } class C_a : A {} class B_a : B!C_a { // Implement this method in B_a. void foo(C_a c); } // Add another class which descends from the A interface. class X_a : A {} void main() { B_a b = new B_a; // Let's imagine that the compiler allowed this. B!A b_a = b; // Now, create an X_a. X_a x = new X_a; // This will be allowed! b_a.foo(x); writeln(b_a); } //////////////////////////////// The line "b_a.foo(x);" calls B_a.foo, which takes a C_a parameter, with an X_a parameter, which is actually an unrelated type. So, this would break the type system.