Given the following code:
type
MyType = ref object of RootObj
testInt: int
proc intSumStatic(x,y: MyType): int =
x.testInt + y.testInt
method intSumDynamic(x,y: MyType): int {.base.} =
x.testInt + y.testInt
static:
var x = MyType(testInt: 5)
var y = MyType(testInt: 3)
echo "compiletime static proc : " & $x.intSumStatic(y)
echo "compiletime dynamic method: " & $x.intSumDynamic(y)
, the output when compiling is:
compiletime static proc : 8
compiletime dynamic method: 0
I understand that it makes no sense to call a method at compile time, but shouldn't I expect a "cannot evaluate at compile time" error then?Just as info for other Nim-novices like me: It is even possible to call a method at compile time, using procCall and explicitly casting to the desired type. In my given example, it could be done like so:
echo "compiletime procCall : " & $(procCall MyType(x).intSumDynamic(MyType(y)))
This is awesome, because it solves the original problem I was puzzling with while I encountered the behavior described in my initial post.