Is it possible to override whatever method is used when an object of a custom class is converted to a string for display?
For example, this code currently prints (x: 4, y: 5)
, but I want it to print just (4,5)
type Point = object
x: int
y: int
let p = Point(x:4, y:5)
echo p
What proc/method/whatever do I implement to change the default Point->string conversion used by echo
?
Figured it out; echo
's docs says you just gotta overload the $
operator:
from strformat import fmt
type Point = object
x: int
y: int
proc `$`(point: Point): string = fmt"({point.x}, {point.y})"
let p = Point(x:4, y:5)
echo p # prints "(4, 5)"