Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a smalltalk "toString" equivalent?

For example, let's say I am adding a set of objects I created, called myClass (which contains a string) to myList and then I wish to use the list and pass it to a GUI list. Is there a way to populate that GUI list only with the value of that string in myClass?

like image 506
laluser Avatar asked Mar 23 '11 21:03

laluser


People also ask

What type of method is toString ()?

What is toString()? A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.

In what class would one always find the method toString ()?

The toString method is implemented by default in the class Object (docs.oracle.com/javase/7/docs/api/java/lang/…). This method is not abstract so you don't have to override it. You can if you want to change the string representation of you object.

What is the equivalent of toString in Python?

In Python, the equivalent of the tostring() is the str() function. The str() is a built-in function. It can convert an object of a different type to a string. When we call this function, it calls the __str__() function internally to get the representation of the object as a string.

Is toString automatically called?

JavaScript calls the toString method automatically when a Function is to be represented as a text value, e.g. when a function is concatenated with a string.


1 Answers

Every object should respond to printString message with some meaningful string presentation of itself. You can write your own #printString for your object and use it.

Even better for your case could be implementation of asString conversion method, which converts your object to a string.

like image 92
Janko Mivšek Avatar answered Sep 28 '22 00:09

Janko Mivšek