Hello!
I've encountered a method related problem at the compile time. Take a look at the code:
type
AnimalObj* = object of RootObj
Animal* = ref AnimalObj
Cat* = ref object of AnimalObj
Dog* = ref object of AnimalObj
method kind(o: Animal): string {.base.} =
"Animal"
method kind(o: Cat): string =
"Cat"
method kind(o: Dog): string =
"Dog"
proc newCat*(): Cat =
new(result)
proc newDog*(): Dog =
new(result)
static:
let animals: seq[Animal] = @[newCat(), newDog()]
echo "Compile time:"
for animal in animals:
echo animal.kind().repr
echo "Run time:"
let rtanimals: seq[Animal] = @[newCat(), newDog()]
for animal in rtanimals:
echo animal.kind().repr
After running it I've got the following result:
$ nim c --run mwe.nim
...
Compile time:
""
""
...
Run time:
0x7f36c62d4050"Cat"
0x7f36c62d4078"Dog"
So, looks like methods are not working at the compile time. Is it a bug?
According to the manual, you cannot evaluate methods at compile time.
"other optimizations like compile time evaluation or dead code elimination do not work with methods."