I try to do this:
template eval(body: untyped) =
let res = body
if isStringifyable(res):
echo res
else:
discard
I have known typeinfo in stdlib or concept may be helpful, but failed to use them successfully. I find no documentations for concepts. I wrote this with AI:
type Stringifyable[T] = concept
proc `$`(x: T): string
But
assert ((proc()=discard) is Stringifyable) == true
while procedures actually can not be turned into strings.Well, I find something in https://github.com/nim-lang/RFCs/issues/164 . I make mine like this:
proc isStringifyableImpl[T](body: T): bool =
type Helper = concept
proc `$`(x: T): string
result = body is Helper
template isStringifyable(body: untyped): bool =
let res = body
isStringifyableImpl[typeof(res)](res)
dump isStringifyable(1) # isStringifyable(1) = true
dump isStringifyable(proc () = discard) # isStringifyable(proc () = (discard )) = false
Seems working.Try
type Stringifyable = concept
proc `$`(x: Self): string
Or with old-style concepts:
type Stringifyable = concept x
$x is string