This simple program doesn’t compile with version 0.18 of the compiler (it worked in some previous version).
type
A = ref object of RootObj
B = ref object of A
val: seq[A]
method `==`(obj1, obj2: A): bool {.base.} = quit "to override!"
method `==`(obj1, obj2: B): bool = obj1.val == obj2.val
The compiler complains that '==' can have side effects. And I don’t see why. If I replace the comparison by a check on length, then a comparison of each element, it compiles.
Obviously, there is something I’m missing. Can somebody enlighten me? Thanks!
I don’t know why it once compiles, though.
The compiler follows the spec more and more. ;-)
The problem is that methods are dynamic and so the compiler doesn't know what == will really be called. The solution is to mark the base method as noSideEffect explicitly so that the compiler can know (and enforce!) that all your == have no side effect.
@mratsim
To raise an exception seems a good idea indeed, a lot better than to quit anyway.
@Araq
The problem was that in my code I didn’t used "quit" as in this example, but a simple comparison as in the modified example. So, there was no side effect. What was missing was to mark explicitly the method with "noSideEffect" and I didn’t understood this at first. But this behavior is fine for me. There are indeed things that the compiler cannot guess.