[BlitzMax 1.50] Incosistency between source and code

Community Forums/Bug Reports/[BlitzMax 1.50] Incosistency between source and code

col(Posted April) [#1]
Blitzmax 1.50

Theres an inconsistency between the source code written and data returned with arrays of types cast from an array of objects.

The code below shows the issue at the last few lines that use the .debug method. The con.getdata() is return an array of objects that are of typeb. In the code you can see that I'm casting to an array of typea. When you run the code it does indeed call into the .debug method of typeb. I would have thought the array cast should return Null? On top of which the actual type of each element in the returned array is of typeb, but as you can see I'm using a loop of typea to access each element :/


Strict

Type typea
	Field a_a:Int
	Field a_b:Int
	Field a_c:Int
	
	Method debug()
		Print "typea"
	EndMethod
EndType

Type typeb
	Field b_a:Int
	Field b_b:Int
	Field b_c:Int
	
	Method debug()
		Print "typeb"
	EndMethod
EndType

Type container
	Field data:typeb[]
	
	Method New()
		For Local i:Int = 0 Until 10
			data :+ [New typeb]
		Next
	EndMethod
	
	Method getdata:Object[]()
		Return data
	EndMethod
EndType



Local con:container = New container
Local data:typea[] = typea[](con.getdata())

For Local t:typea = EachIn data
	Print t.a_a
	t.debug()
Next



grable(Posted June) [#2]
The function bbArrayCastFromObject in brl.mod/blitz.mod/blitz_array.c is the one dealing with array casting.
And it seems to treat ALL Objects as interchangable.

Note that when it comes to For Each the type of the array doesnt matter, since it casts each element to the type specified either way.

I dunno if its a bug or not though, but i can see why it does what it does, since all it has to work with are stringified type names.
Meaning it has no way to check if two types are compatible other than equality, or searching the entire type table for both type names and then doing a type check.
Which i would image could be rather slow.

But, if you still want that behavior, Replace it with this :) Im just not sure if the upcast is the correct way to do it...