blitzmax icon indicating copy to clipboard operation
blitzmax copied to clipboard

Array casting issue

Open davecamp opened this issue 7 years ago • 1 comments

I have found an issue with casting to and from 'type array' and 'Object array'. In this example I create an array of 'typeb'. I get the array by 'Object[]'. I can then cast to an array of 'typea[]' - which I would expect to be invalid and end up as 'Null'? I can then interate throught the returned and cast array using an iterator of 'typea' which of course shows me the data from a 'typeb' - as the array was originally a 'typeb[]'. I'm not always so good with explaining so here's the example...

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()) ' This Local data appears valid but is actually a 'typeb[]'

For Local t:typea = EachIn data
	Print t.a_a ' I can access the 't' using typea fields but they are actually referencing a 'typeb' object
	t.debug()
Next

davecamp avatar Apr 19 '17 09:04 davecamp

A safe implementation of such casts may be possible with Reflection or suggestions in the community forum thread may help work around this known limitation of BlitzMax.

nitrologic avatar Jun 02 '17 23:06 nitrologic