Return List or Array
Monkey Forums/Monkey Programming/Return List or Array
| ||
| Can i use Return to get a List or an Array? Can it only be used with 0's and 1's? |
| ||
| Yes you can, you can get a method or a function returning anything you like: Ints: Method GetX:Int() Local i:Int = 5 Return i End Strings: Method GetName:String() Local i:String = "Steve" Return i End Lists: Method GetList:List<Spark>() Local i:=New List<Spark> Return i End User Object: Method GetList:Player() Local player:Player =New Player Return player End |
| ||
| Ok thanks. I think i am getting the syntax wrong on the receiving end. If i try to say Field ReceivingIntList:IntList = New IntList ReceivingIntList = GetIntList() Method GetIntList() Local a:IntList = New IntList a.Addlast(1) Return a end This is basicly what i am doing, but i get "Cannot convert IntList to Int" error. |
| ||
Method GetIntList : IntList() Local a:IntList = New IntList a.Addlast(1) Return a end try the above, you need to tell the method that you intend to return a list. |
| ||
To help these kind of errors, put "Strict" at the top of your code. It will force you to put a return type in every method/function, if there isnt a return value you need to use "Void".
Strict
Function Main:Int()
New Game()
Return 0
End
Class Game
Field x:Float
Method New()
x = 10.5
End
Method GetX:Float()
Return x
End
Method MovePlayer:Void()
x += 10
End
End
|
| ||
| Oh cool. Thanks guys. |