Hi, I don't know if I expressed my issue correctly but it's easier to understand it with code. In nim you can overload every generic proc pretty easily, it picks the most specific overload. Think of the $ operator. But how do I know users have actually overloaded $? This is what it looks like:
proc foo[T](x: T) =
when defined(`$` for T): # call $x
else: # do something else instead of calling generic `$`
I think the only option is requiring the user to define a template hasDollar: bool = true ?
proc foo[T](x: T) =
when compiles $x:
echo 1
else:
echo 0
type A = distinct int
foo A 1
I tried wrapping it in a template but there is an issue with cointainers:
template defineOverload(value: untyped, typ: typedesc, body: untyped) =
proc change(value: var typ) =
body
template hasOverload(t: typedesc[typ]): bool = true
defineOverload(x, seq[int]):
x = @[]
type
Foo[T] = object
x: T
defineOverload(x, seq[Foo[T]]): # how?
x = @[]
Extremely ugly hack, but seems to work:
template hasCustomStrImpl(x): bool =
`not`: compiles:
proc `$`(_: typeof(x)): string = ""
discard $x