Add objprint
Add ability to print (ref) objects/tuples
This commit is contained in:
parent
6ea88b1a33
commit
e96cd5abe2
1 changed files with 40 additions and 0 deletions
40
src/extensions/objprint.nim
Normal file
40
src/extensions/objprint.nim
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import strutils
|
||||||
|
import typetraits
|
||||||
|
|
||||||
|
proc typeToStr[T](some: typedesc[T]): string = name(T)
|
||||||
|
|
||||||
|
template tupleObjToStr(obj): string =
|
||||||
|
mixin typeToStr
|
||||||
|
var res = typeToStr(type(obj))
|
||||||
|
template helper(n) {.gensym.} =
|
||||||
|
res.add("(")
|
||||||
|
var firstElement = true
|
||||||
|
for name, value in n.fieldPairs():
|
||||||
|
when compiles(value):
|
||||||
|
if not firstElement:
|
||||||
|
res.add(", ")
|
||||||
|
res.add(name)
|
||||||
|
res.add(": ")
|
||||||
|
when (value is object or value is tuple):
|
||||||
|
when (value is tuple):
|
||||||
|
res.add("tuple " & typeToStr(type(value)))
|
||||||
|
else:
|
||||||
|
res.add(typeToStr(type(value)))
|
||||||
|
helper(value)
|
||||||
|
elif (value is string):
|
||||||
|
res.add("\"" & $value & "\"")
|
||||||
|
else:
|
||||||
|
res.add($value)
|
||||||
|
firstElement = false
|
||||||
|
res.add(")")
|
||||||
|
helper(obj)
|
||||||
|
res
|
||||||
|
|
||||||
|
proc `$`*(s: object): string =
|
||||||
|
result = tupleObjToStr(s).replace(":ObjectType", "")
|
||||||
|
|
||||||
|
proc `$`*(s: ref object): string =
|
||||||
|
result = "ref " & $s[]
|
||||||
|
|
||||||
|
proc `$`*(s: tuple): string =
|
||||||
|
result = tupleObjToStr(s)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue