Generic and string casting

Monkey Forums/Monkey Programming/Generic and string casting

sereschkin(Posted 2015) [#1]
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



ziggy(Posted 2015) [#2]
To check for the type of an instance, use the class name: Object(T) will be <> null if T is an Object


sereschkin(Posted 2015) [#3]
Ok, I didn't know that. Thank you. But if I try...

If Object(element)<>Null
    value+=element
Endif


...the parser will throw another error: Cannot convert from Int to object.
And this because of that line:

SomeClass<Int>.SomeMethod(myList)



ziggy(Posted 2015) [#4]
yes, this is because of autoboxing. ints aren't objects, so can't be casted. When you create a generic for an int, you're really creating it for a boxed int


sereschkin(Posted 2015) [#5]
So, there is no solution like writing just one single method for a string conversion for all types?


ziggy(Posted 2015) [#6]
Yes! ToString is already a string conversion function for all types. Any type implementing ToString can be used for automatic ToString conversions.
Just add a Method ToString:String() method on any object that needs to provide automatic to string conversion. You could then do things like Print myObejct, etc...


sereschkin(Posted 2015) [#7]
Thanks ziggy for input. I'l think about your suggestions and hints. The solution is near. I feel it^^.