I can't figure out how to write a simple method that can handle a string conversion from every type to string. Ok, I know that not everything is convertible to string, but I would like to check if it is possible and then do the conversion.
Following example:
Class SomeClass<T>
...
Function SomeMethod:Void()
Local elements:List<T> = List<T>(myObjectVarialbe)
Local index:Int
Local value:String
For Local element:T = Eachin elements
value+=element
Next
End
End
If I use it in that way:
SomeClass<Int>.SomeMethod()
it works. But this does not:
SomeClass<Object>.SomeMethod()
Because it is not possible to convert object to string. What I would like to get to work is the following (pseudo code)
Class SomeClass<T>
...
Function SomeMethod:Void()
Local elements:List<T> = List<T>(myObjectVarialbe)
Local index:Int
Local value:String
For Local element:T = Eachin elements
If T is Object
' do some stuff with object
Endif
If T is Int
value+=String(element)
Endif
Next
End
End
But the compiler don't let me do this. It just don't compile. It throws an error saying that object can't be converted to String for T = Object. Is there a chance to write one single method for handling all kind of types (i.e. T=*) in monkey?
Here is a "running" sample, that does not work:
Class SomeClass<T>
Function SomeMethod:Void(myObjectVarialbe:Object)
Local elements:List<T> = List<T>(myObjectVarialbe)
Local index:Int
Local value:String
For Local element:T = Eachin elements
value+=element
Next
End
End
Function Main:Int()
Local myList:List<Int> = New List<Int>([1,2,3])
SomeClass<Int>.SomeMethod(myList)
SomeClass<Object>.SomeMethod(myList)
Return 0
End
|