Re:Never Use toString() for Behaviour
by flerchin on Wednesday December 04, 2019 @10:53AM
(#21703213)
We use lombok, and the toString annotation is immensely helpful for logging and avoiding this type of thing.
[ Parent
| on HN ]
Re:Never Use toString() for Behaviour
by fahadkhan on Wednesday December 04, 2019 @11:36AM
(#21703681)
How does Lombok help to avoid this?
Re:Never Use toString() for Behaviour
by nsporillo on Wednesday December 04, 2019 @02:24PM
(#21705944)
Lomboks annotation @ToString will automatically generate the typical overridden toString() method based on the fields.
I just tried this in Java and I ran quickly into circularity issues (one of the fields has an indirect reference to the same object, say a list which the current instance is part of). How does Lombok cope with that?
It's not that easy as I thought (see my sibling comment).
Even Object.toString could do this using reflection. If you're worried about the performance, override it. I never understood why this is not the case (doesn't Haskell do something similar?).