Print and ToString()?

Monkey Forums/Monkey Programming/Print and ToString()?

TheRedFox(Posted 2011) [#1]
Is there any way to do this:

Function InfoList(msg:String, l:ArrayList<Object>)
    Print msg
	For Local e:Object = Eachin l
	  Print e.ToString()
	Next
End


or even

Print e


?


Jesse(Posted 2011) [#2]
definitely No.
while it's obvious to you what you want it to print there is nothing indicating the compiler what you want to output.

you can create a base type add a function to each extended type. it can be called "ToString" that contain anything you want to print. Store all of the objects in the array as the base class then you can call a function as you did above but passing the objects as the base class instead of "Object". and it will do what you want.


TheRedFox(Posted 2011) [#3]
Yep. In the meantime, I had a look at how monkey does it internally and it is clear that it will not work out of the box.

Would you mean using a Printable interface and doing something like (code below just for the conceptual)? Seems like the cleanest way.

Interface Printable
  Method ToString:String()
End

Function InfoList(msg:String, l:ArrayList<Printable>)
    Print msg
	For Local e := Eachin l
	  Print e.ToString()
	Next
End

Class MyThingy Implements Printable
   Field stuff:String
   ...
   Method ToString():String
     Return stuff 
   End
End




Jesse(Posted 2011) [#4]
I am not too familiar with interface so I am not too sure if that is 100% usable but it looks workable in my opinion.


NoOdle(Posted 2011) [#5]
double post


NoOdle(Posted 2011) [#6]
Slight error in your ToString method in the Class MyThingy, it should say: Method ToString:String()

I need to check out implements and interface, looks handy for adding on component functionality. How many implements can a class have? Do you just put a comma after the first?

Off to check the docs to see what I can find...

Nice, the docs had something, not sure how I missed that!


Interfaces:

In interface is similar to a class, except that it can only contain constants and abstract methods.
Classes can implement an interface using the Implements keyword in the class declaration.
Classes that implement an interface must declare each method declared in the interface.
An interface can be used where ever a class is expected, for example when declaring the types of variables, or function return types. And interface cannot however be used with New.
An Interface can also optionally extend existing interfaces, in which cases all methods in all extended interfaces must be declared by any implementing classes.
Interfaces can not be generic.
The syntax for declaring an interface is:

Interface identifier [ Extends Interfaces ]
...Member declarations...
End

All methods declared inside an interface are automatically treated as abstract, and can therefore have no 'body'.
Here is an example of using interfaces:



Interface Moveable
	Method Move()
End Interface


Interface Drawable
	Method Draw()
End Interface



Class Actor Implements Moveable, Drawable

	Method Move()
		Print "Actor.Move()"
	End Method
        
	Method Draw()
		Print "Actor.Draw()"
	End Method
	
End Class


Function Move( moveable:Moveable )
	moveable.Move
End Function

Function Draw( drawable:Drawable )
	drawable.Draw
End Function


Function Main()
	Local actor := New Actor
	Move actor
	Draw actor
End Function



Samah(Posted 2011) [#7]
It's a pity, because the Java top-level Object class has an overridable toString() method. Most I/O classes have the ability to take an Object and automatically call toString() on it.


AdamRedwoods(Posted 2011) [#8]
Would this require some type of reflection?


TheRedFox(Posted 2011) [#9]
I would require the Monkey object to have a ToString:String() but I can understand that for cross platform, it is best no to.

For debugging purposes, it is great to have a ToString for sure.

But the Printable trick is good enough for me. Maybe could we decide on a "special name" in the community so that we could have a space for extensions that would work nicely together.

What about:


Interface MonkeyExtendable
   Method ToString:String() Abstract
End

Class MonkeyObject Extends Object Implements MonkeyExtendable
  ...     
End

Global collection:ArrayList<MonkeyObject>



We could then work a bit on contexts and so on, a bit like in Java's Spring and avoid tons of globals and lookups.


Beaker(Posted 2011) [#10]
Inheritance and Interfaces seem like massive overkill here. You can just add a ToString method and/or function for every class. Nice and simple. Unless someone can think of a reason to do it another way?


Samah(Posted 2011) [#11]
@Beaker: Inheritance and Interfaces seem like massive overkill here. You can just add a ToString method and/or function for every class. Nice and simple.

Except it's not. You can't just call .ToString() on any arbitrary class. If it doesn't have that method, it won't compile. You would need an overloaded Print function for each class in your application. To have a single Print function requires every class to have a common ancestor. Object doesn't fit the bill since it doesn't support the ToString() method.
The point of an interface is to give you a declarable type that you know will support one or more specific methods.


Beaker(Posted 2011) [#12]
Sorry, I think I must've missed the original point here.


Samah(Posted 2011) [#13]
What we need to do is get Mark to add a ToString method to Object. An Equals and/or CompareTo method would be nice too, so that you could theoretically use any Object as the key for a Map without having to extend it.


TheRedFox(Posted 2011) [#14]
+10! (that's a factorial...)