With the 'Child' Type inheriting from the 'Parent' Type:
Bug example:
Type Parent
Dim As Integer I = 1
End Type
Type Child Extends Parent
Dim As Integer J = 2
Dim As Integer K = 3
End Type
Sub print_parent_element (Byref item As Parent)
Print " " & item.I;
End Sub
Sub print_parent_array (array() As Parent)
For n As Integer = Lbound(array) To Ubound(array)
Print " " & array(n).I;
Next n
Print
End Sub
Dim As Child array(0 To 6)
For n As Integer = Lbound(array) To Ubound(array)
print_parent_element(array(n)) '' OK (covariance)
Next n
Print
print_parent_array(array()) '' NOK (no covariance), expected 'error 58: Type mismatch, at parameter 1 ...'
Print
Sleep
Output:
1 1 1 1 1 1 1
1 2 3 1 2 3 1
See also on forum the post at:
https://www.freebasic.net/forum/viewtopic.php?p=264415#p264415
and the following posts.
When passing (to a procedure) an array of derived-type elements instead of base-type elements, a simple fix could be applied to fix the problem of address calculation of elements:
From the previous example thus modified (see the 'print_parent_array(() As Parent))' Sub)
For fbc rev 1.07, use 'fb_ArrayGetDesc()' instead of 'FBC.ArrayDescriptorPtr()' for fbc rev 1.08 as here:
Output:
Therefore using:
p = pd->index_ptr + pd->element_len * n
instead of:
p = pd->index_ptr + sizeof(Parent) * n
Last edit: fxm (freebasic.net) 2019-09-22
This could be even applied systematically to all arrays passed as parameters to procedures.
This would solve other problems like passing a zstring array to a procedure .
See the other bug reports BUG #650 and BUG #867.
In fbc version 1.10, as of commit [3eef323cdc9a866a0c9bbc130f88950d35363f00] with the fix of [#817], the type mismatch is detected and the error message is given.
Related
Bugs:
#817Commit: [3eef32]