Hi,
I try to write a generic proc which should have different implementations depending if another proc with a given type signature exists or not. Something like:
proc foo(x: int) = nil
proc foo(x: float) = nil
proc bar[T] (v: T) =
when exists(foo(T)) :
echo "specific foo exists, it's safe to call it"
foo(v)
else : echo "foo (T) does not exist"
bar(1)
bar(3.14)
bar("Hi")
is this possible ?
var x: T
when compiles(foo(x)): echo "specific foo exists, it's safe to call it"
But its priority is low on my todo list.
template supports(opr, x: expr): bool {.immediate.} =
compiles(opr(x)) or compiles(opr(x, x))
echo supports(`++`, 34)
echo supports(`not`, true)
echo supports(`+`, 34)
I have just updated from github - cool !!
many thanks.
No, but it still has bugs... It's hard to undo every side-effect in the compiler that 'compiles' might trigger. I improved it though. In fact, I'm unsure what the spec should say about system.compiles. I think the spec should list valid usages and declare all other usages a "quality of implementation" issue. :D
BTW don't waste your time to come up with a tiny test case to demonstrate the problem. Input length is rarely relevant when debugging the compiler. ;-)