Array strangeness

Monkey Forums/Monkey Programming/Array strangeness

GW_(Posted 2011) [#1]
I'm confused by this..
There is nothing in the docs that would explain why this bombs out.

This works..
Function Test:Void(num:Int)
	Local c:Int = num
	Local ap:Float[]

	ap = New Float[c]
End


This fails...
Function Test2:Void(num:Int)
	Local c:Int = num
	Local ap:Float[][]	
	ap = New Float[c][3]
End



matt(Posted 2011) [#2]
I don't think multidimensional arrays are possible.


Canardian(Posted 2011) [#3]
I think you need to make a class for the data type, and then use that as array element.


Richard Betson(Posted 2011) [#4]
No multidimensional arrays in Monkey.


GW_(Posted 2011) [#5]
WTF?
Why would there be no multi dim arrays?
All the target languages support them.
The Monkey docs show examples of multi dim arrays.
I find it hard to believe that mark would cripple the language that way.


Xaron(Posted 2011) [#6]
Well Mark wrote in his blog: http://marksibly.blogspot.com/2010/08/self-compiling.html


* Multidimensional arrays are out, although you can still have arrays of arrays, eg:

Local t[10,20] 'OK in bmx1, Error in bmx2
Local t[][10] 'OK in bmx1 and bmx2.


* Arrays can not be 'downcast' in bmx2, eg:

Local t:Object[]=New Thing[10]
Local p:Thing[]=Thing[]( t ) 'OK in bmx1, Error in bmx2.

This will mean that arrays may have to be manually 'wrapped' in some situations.



GW_(Posted 2011) [#7]
Xaron, whats the point of your post?
I've never tried to use bmax style arrays. Look at my examples again. I'm using the C style declaration, which is compatible with the docs and Marks blog posts.


Xaron(Posted 2011) [#8]
Looking at your failed example you obviously did not. ;)

So your creation of a two-dimensional array fails because it is simply not supported that way. That is what Mark writes in his example as well. No offense here, sorry if I was a bit rude.


GW_(Posted 2011) [#9]
congrats! your the monkey forums first troll.

I hope Mark can clear up this array business.


Xaron(Posted 2011) [#10]
Could you elaborate why? Maybe I just missed your point...


Xaron(Posted 2011) [#11]
What do you think you are doing here?

New Float[c][3]


GW_(Posted 2011) [#12]
because the array is declared with [][] not [,].
That's compatable with the docs and marks blog post.
Nowhere is it stated that arrays with 2 dimensions "[][]" are not supported. Thats a pretty huge deal, I think it would have come up..


Beaker(Posted 2011) [#13]
This works:
Import mojo

Class ArrayExample Extends App
	
	Method OnCreate()
		SetUpdateRate 30
		
		Local blah:Int[2][]
		
		Local arr1:Int[4]
		arr1 = [0,1,2,3]
		
		Local arr2:Int[4]
		For Local f:=0 To 3
			arr2[f] = (f+1)*10
		End
		
		blah[0] = arr1		
		blah[1] = arr2
		
		For Local f:= 0 To 1
			For Local f2:=0 To 3
				Print blah[f][f2]
			Next
		Next
		
		
	End

	Method OnUpdate()
	End
	
	Method OnRender()
		Cls 50,50,50
		
	End

	
End

Function Main()
	New ArrayExample
End



Beaker(Posted 2011) [#14]
For the record I think these are called jagged arrays.


Xaron(Posted 2011) [#15]
Thanks Beaker, very interesting!

@_GW: Sorry it was not my intention to troll. :( I guess I simply misunderstood something here...


wiebow(Posted 2011) [#16]
You werent trolling, m8.