Remark on point 1):
(see also https://sourceforge.net/p/fbc/feature-r ... /289/#6751
- To authorize the covariance overriding , the type check at compile time must be relaxed to accept not only the return type corresponding to the first abstract/virtual function declaration but also all subtypes corresponding to the overriding function declarations.
- In order to keep a safe typing, a runtime type checking code could be added by compiler in order to return a runtime error message if there is no compatiblity between return type and caller use (super-derived usage forbidden at caller level).
- I think that for comparing (at runtime) the type used by caller with those of the hierarchy of return types, it is necessary that all have a Run-Time Type Information (RTTI).
- It might be well to already impose this condition at compilation time (the virtual function return types derived from 'Object') to accept the covariance overriding.
Code: Select all
Type A Extends Object
Type B Extends A
Type C Extends B
Type Parent Extends Object
Declare Virtual Function f () As A Ptr
Function Parent.f () As A Ptr
Print "Parent.f() As A Ptr"
Return New A
Type Child Extends Parent
Declare Virtual Function f () As B Ptr Override
Function Child.f () As B Ptr
Print "Child.f() As B Ptr"
Return New B
Type GrandChild Extends Child
Declare Virtual Function f () As C Ptr Override
Function GrandChild.f () As C Ptr
Print "GrandChild.f() As C Ptr"
Return New C
Dim As Child ch1
Dim As Parent Ptr ppc = @ch1
Dim As A Ptr pa = ppc->f() '' must be allowed at compile time and runtime
Dim As B Ptr pb = ppc->f() '' must be allowed at compile time and runtime
Dim As C Ptr pc = ppc->f() '' must be allowed at compile time but not at runtime
.....\FBIde0.4.6r4_fbc1.02.0\FBIDETEMP.bas(40) error 180: Invalid assignment/conversion in 'Dim As B Ptr pb = ppc->f()'
.....\FBIde0.4.6r4_fbc1.02.0\FBIDETEMP.bas(41) error 180: Invalid assignment/conversion in 'Dim As C Ptr pc = ppc->f()'
For override procedures in C++:
- I think that the covariance on return type (pointer or reference) works and fully (including when assigning).
- No covariance/contravariance on parameter.
- On Jan 11, 2015: remark complemented.