Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default stringify for objects, equivalent to Java's toString?

I have just take a look at the 3º tutorial from dart, creating the rating component. I was wondering if there is same method which is called when stringifying an object, something similar to Java's toString.

For example:

MyClass myObject = new MyClass();
System.out.println(myObject);

Will call MyClass.toString() if overwriten, else will call it's parent until java.lang.Object is reached giving a default toString.

I find kind ugly (completely subjective) doing:

<span ng-repeat="star in cmp.stars" > {{star.toString()}} </span>

I would rather do:

    <span ng-repeat="star in cmp.stars" > {{star}} </span>

And give the implementation of how I want it to display at an averwritten method. Is this possible?

like image 952
Javier Mr Avatar asked Apr 07 '14 19:04

Javier Mr


People also ask

What is the default toString method?

By default the toString() method will return a string that lists the name of the class followed by an @ sign and then a hexadecimal representation of the memory location the instantiated object has been assigned to.

Does object have a toString method?

toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.

What is toString () in Javascript?

Definition and Usage. The toString() method returns a string as a string. The toString() method does not change the original string. The toString() method can be used to convert a string object into a string.

What is to toString () method for?

Java - toString() Method The method is used to get a String object representing the value of the Number Object. If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned.


3 Answers

If you have something like this:

class MyClass {
    String data;

    MyClass(this.data);

    @override
    String toString() {
        return data;
    }
}

MyClass myObject = new MyClass("someData");
print(myObject); // outputs "someData", not 'Instance of MyClass'

I think this might be what you are looking for.

like image 159
dgp Avatar answered Oct 12 '22 21:10

dgp


Yes it works like this for print, String interpolation or Angular mustaches.

By overriding the String toString() method on your object the displayed value will be the result of this toString() call. If there's no toString() defined in the class hierarchy the toString() of Object will be called (which will return Instance of 'MyClass' for class MyClass{}).

like image 28
Alexandre Ardhuin Avatar answered Oct 12 '22 22:10

Alexandre Ardhuin


You may be interesting look how Rating component was implemented in Angular Dart UI project. Check this out.

Sergey.

like image 1
akserg Avatar answered Oct 12 '22 21:10

akserg