I'm trying to override the toString method in Scala. It has to return the time passed since an instance of the class is created. The time passed is computed in timePassed() method.
I have no idea on how to force the toString method to execute the timePassed method and generate an appropriate results when I call it.
toString isn't magic, it's free to call whatever methods it wants, have local variables, etc.
override def toString = s"The LED has a status of ${timePassed()}."
or
override def toString = {
val internalTime = timePassed()
s"The LED has a status of $internalTime."
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With