I'd like this thread to be some kind of summary of pros/cons for overriding and calling toString with or without empty parentheses, because this thing still confuses me sometimes, even though I've been into Scala for quite a while.
So which one is preferable over another? Comments from Scala geeks, officials and OCD paranoids are highly appreciated.
Pros to toString:
- seems to be an obvious and natural choice at the first glance;
- most cases are trivial and just construct Strings on the fly without ever modifying internal state;
another common case is to delegate method call to the wrapped abstraction:
override def toString = underlying.toString
Pros to toString():
- definitely not "accessor-like" name (that's how IntelliJ IDEA inspector complains every once in a while);
- might imply some CPU or I/O work (in cases where counting every
System.arrayCopycall is crucial to performance); - even might imply some mutable state changing (consider an example when first
toStringcall is expensive, so it is cached internally to yield quicker calls in future).
So what's the best practice? Am I still missing something?
Update: this question is related specifically to toString which is defined on every JVM object, so I was hoping to find the best practice, if it ever exists.