modifying unidentified array in a function
BlitzMax Forums/BlitzMax Programming/modifying unidentified array in a function
| ||
I would like to create a function Append for example that can add an object at the end of an array provided that the object passed is of the same type as the objects already in the array... At first I thought of doing this:Function Append(t:Object[] Var,o:Object) Local size:Int = Len(t) t = t[..size+1] t[size] = o End Function and then have the following code: Type Any_Type Field x:Int,y:Int Method Any_Method() 'Any code here End Method '... End Type Local My_Array:Any_Type[] Local AT:Any_Type = New Any_Type AT.x = 5 AT.y = 6 Append(My_Array, AT) But this doesn't work: it returns a problem of conversion... the only solution I found was to do things like this: Function Append:Object[](t:Object[],o:Object) Local size:Int = Len(t) t = t[..size+1] t[size] = o Return t End Function And use the Append function in the following way: Type Any_Type Field x:Int,y:Int Method Any_Method() 'Any code here End Method '... End Type Local My_Array:Any_Type[] Local AT:Any_Type = New Any_Type AT.x = 5 AT.y = 6 My_Array = Any_Type[](Append(My_Array, AT)) This works fine but the way you call the Append function is a bit annoying... considering I made a whole module with functions like Append, Insert, Remove, Concat and so on... I would have preferred an easier way of calling Append... Any idea on how Append should be coded so that it only needs to be called this way: Append(t:array, o:myobject) (<-- pseudo code of course) |