Question about custom classes and arrays

Monkey Forums/Monkey Programming/Question about custom classes and arrays

Chroma(Posted 2011) [#1]
Hi. Can someone take a peek at this and tell me why array [0] x and y come back as undefined? Possibly a bug.

Strict

Import mojo

Function Main:Int()
	
	Local vecArray:Vec2[]
	vecArray += [New Vec2(0, 1)]
	
	Print vecArray[0].x
	Print vecArray[0].y

	Return 0
End Function

Class Vec2
	Field x:Float
	Field y:Float
	
	Method New (x:Float, y:Float)
		Self.x = x
		Self.y = y
	End Method

End Class



Sledge(Posted 2011) [#2]
+=???

Strict

Import mojo

Function Main:Int()
	
	Local vecArray:Vec2[]
	vecArray = [New Vec2(0, 1)]
	
	Print vecArray[0].x
	Print vecArray[0].y

	Return 0
End Function

Class Vec2
	Field x:Float
	Field y:Float
	
	Method New (x:Float, y:Float)
		Self.x = x
		Self.y = y
	End Method

End Class



Chroma(Posted 2011) [#3]
?? That doesn't add a new array. The += has a purpose!


Chroma(Posted 2011) [#4]
Here, this will better explain the problem. And the array length comes back as 15. Which doesn't jive at all.


Strict

Import mojo

Function Main:Int()
	
	'for comparison
	Local intArray:Int[]
	intArray += [1]
	intArray += [2]
	Print "First value:"+intArray[0]
	Print "Second value:"+intArray[1]
	Print "intArray Length:"+intArray.Length
	
	
	Local vecArray:Vec2[]
	vecArray += [New Vec2(0, 1)]
	Print "vec x:"+vecArray[0].x
	Print "vec y:"+vecArray[0].y
	Print "vecArray Length:"+vecArray.Length

	Return 0
End Function

Class Vec2
	Field x:Float
	Field y:Float
	
	Method New (x:Float, y:Float)
		Self.x = x
		Self.y = y
	End Method

End Class



Sledge(Posted 2011) [#5]
EDIT: Oh, hang on you posted a new example while I was typing -- lemme see...


Sledge(Posted 2011) [#6]
Is...
intArray += [2]
...supposed to be shorthand for...
intArray.Resize( intArray.Length()+1 )
intArray[intArray.Length()-1]=2
...? I didn't know you were supposed to be able to use the += operator with arrays, but I'd suggest that a stack might be more appropriate as you can just add new entries and retrieve by index.

EDIT: I see the discrepancy with classes BTW -- having a faff around now.


Chroma(Posted 2011) [#7]
Yep it is short-hand for that. Stack? You mean List?

And was Void taken out? I'm getting a syntax error that it doesn't match any commands etc.


Chroma(Posted 2011) [#8]
Hm...calling a Method from an array isn't working either...

vecArray[0].Update()

...
Class Vec2

   Method Update()
      ...do stuff
   End Method
End Class



Sledge(Posted 2011) [#9]
If += is documented as a valid operator for arrays then I'd run it by the bugs forum. Actually, do it anyway because the bug might be that it works with Ints! :D


Sledge(Posted 2011) [#10]
Hm...calling a Method from an array isn't working either...
No, it's the instantiation of the object that seems to be failing. If you explicitly create a instance in the array then you can call a Method from it (v32).


Chroma(Posted 2011) [#11]
Resize isn't working.

Did a resize to 5 on the array and the length came back 0. Something is up.


Chroma(Posted 2011) [#12]
This prints 0 after the resize.

Local a:Int[]
Print a.Length
a.Resize(10)
Print a.Length



Sledge(Posted 2011) [#13]
Yep -- looks like that's what it all stems from. Nice find!


degac(Posted 2011) [#14]
Sorry, but there's no bug at all (just seen a post in the Bug section)...

The correct syntax is

a=a.Resize(10)

I dont' know that really do Array += [1]... (it outputs '20')...

Import mojo

Function Main:Int()
   Local ZArray:Int[]
   ZArray=ZArray.Resize(10) '<---
   Print "Len: "+ZArray.Length

   ZArray += [1]
   Print ZArray.Length

   Return 0
End Function



Sledge(Posted 2011) [#15]
a=a.Resize(10)
Whoops -- shoulda spotted that from the language reference!


Chroma(Posted 2011) [#16]
Yep, what sucks is that it cost me about 4 hours... :(


GW_(Posted 2011) [#17]
In HTML the array gets converted to some kind of string array. Every other element is a ',' and the number 123 is converted to ['1']['2']['3']

In FLASH it looks like the program doesn't process anything after line: 15

In GLFW It fails to compile because '+=' is not a valid operator.



Strict
Import mojo
Function Main:Int()
   Local ZArray:Int[]
   ZArray=ZArray.Resize(5) '<---

   Print "Len: "+ZArray.Length
	
   For Local i% = 0 Until ZArray.Length
	Print ZArray[i]
   Next
	
   ZArray[4] = 123 	

   ZArray += New Int[1] '[1]
   Print "new Len: "+ZArray.Length


   For Local i% = 0 Until ZArray.Length
	Print ZArray[i]
   Next
	
	
   Print "Done."

   Return 0
End Function



marksibly(Posted 2011) [#18]
Hi,

+= shouldn't be valid with arrays - compiler bug!


GW_(Posted 2011) [#19]
Mark,
Is there another handy shortcut to append to an array like in Bmax?
Arr += [n]


EDIT:
I did some google-ing and it seems that every target language has method of appending to an array.
.push() or vector for c++. Doing it in java is little wonky though.


Samah(Posted 2011) [#20]
I did some google-ing and it seems that every target language has method of appending to an array.
.push() or vector for c++. Doing it in java is little wonky though.

Java's ArrayList class is a List implementation that is backed by a dynamically-sized array.
ArrayList<String> arr = new ArrayList<String>();
arr.add("foo");
arr.add("bar");

Internally, ArrayList has an array field called elementData that expands as needed. It's fairly similar to how std::vector works in C++.