I'd like to be able to use
myobj
and get a custom representation, not the standard (var1: value, var2: value)
.
How can I achieve this? What method (or proc?) do I need to overload?
I tried overloading $ and repr but
myobj
still gave me the default string representation.I'm doing this for now:
proc myrepr(o: Obj): string =
return o.name & ": " & o.info
echo myobj.myrepr
But I would really like to be able to do:
echo myobj ## or echo myobj[]
This is working fine for me:
type
Obj = object
name, info: string
proc `$`(o: Obj): string =
return o.name & " " & o.info
echo Obj(name: "Hi", info: "There")
Works perfectly, thanks.
I had the impression I had tried exactly that but I must have made a mistake somewhere.