Explanation (about enigma #20):
Feature added in fbc rev 1.02.0 (the second change corrected as following should be the true wording in file changelog.txt):
- Relaxed type checking for procedure pointers and virtual method overrides: Procedures with different signatures can now be treated equal if the signatures are compatible. For example: assignments between a function pointer returning an Integer and one returning a Long won't cause a "suspicious pointer assignment" warning on 32bit anymore.
- Covariant parameters and function results (feature request #289)
- Contravariant byref parameters and covariant byref function results (feature request #289)
- Covariance allows you to use a less generic (more derived) type than the one originally specified.
- Contravariance allows you to use a more generic (less derived) type than the one originally specified.
- Invariance requires you to use exactly the same type than the one originally specified.
- Contravariant byref (or by pointer) parameters and covariant byref (or by pointer) results work for assigning procedures to procedure pointers.
- Only the covariant byref (or by pointer) results works for overriding virtual functions, none contravariant parameters is allowed (invariant parameters).
- On the other hand, covariant parameters and contravariant results can not normally be allowed because they are not type safe.
Related documentation pages updated:
- Pointers to Procedures
- VIRTUALExample of assigning to a function pointer a function with a contravariant parameter and a covariant result:
Code: Select all
Dim As Integer I
Type B Extends A
Dim As Integer J
Function f (Byref a0 As A) As B Ptr
Print "Instance of B created"
Return New B(a0)
Dim As Function (Byref As B) As A Ptr pf = @f
Dim As B b0
Dim As A Ptr pab = pf(b0)
Delete Cptr(B Ptr, pab)
Compiler warning message before the rev 1.02.0 of fbc:Example of overriding virtual function with covariant return:(Solution to enigma #20)
warning 4(1): Suspicious pointer assignment
Code: Select all
Type Parent Extends Object
Declare Virtual Function clone () As Parent Ptr
Virtual Function Parent.clone () As Parent Ptr
Print "Instance of Parent cloned"
Return New Parent(This)
Type Child Extends Parent
'--------------- zone beginning for modification ---------------
'Declare Virtual Function clone () As Parent Ptr Override
Declare Virtual Function clone () As Child Ptr Override
'Virtual Function Child.clone () As Parent Ptr
Virtual Function Child.clone () As Child Ptr
Print "Instance of Child cloned"
Return New Child(This)
'----------------- zone ending for modification ----------------
Dim As Parent Ptr ppc1 = New Child
Dim As Parent Ptr ppc2 = ppc1->clone()
Dim As Child Ptr pcc1 = New Child
Dim As Child Ptr pcc2 = pcc1->clone()
Delete Cptr(Child Ptr, ppc2)
Delete Cptr(Child Ptr, ppc1)
Compiler error message before the rev 1.02.0 of fbc:
error 224: Override has different return type than overridden method