In c++
class Preant {
public:
void call() {
}
};
class Child : public Preant {
private:
void call();
};
In nim
type
Preant* = ref object of RootObj
proc call*(this: Preant) =
echo "Call Preant"
type
Child* = ref object of Preant
# Unable to privatize Preant call
proc call(this: Child) =
echo "Call Child"
I don't know how to do it in nim?type
Preant* = ref object of RootObj
proc call*(this: Preant) =
echo "Call Preant"
type
Child* = ref object of Preant
proc call*(this: Child) {.compileTime.} = discard
Now a module that attempts to use them:
import preant
let c = new Child
let p: Preant = c
c.call() # does not compile if this is left uncommented
p.call() # compiles and runs, calling p's call()
Output with c.call() uncommented:
$ nim c use_preant.nim
Hint: used config file '.choosenim/toolchains/nim-1.0.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: use_preant [Processing]
Hint: preant [Processing]
CC: stdlib_system.nim
CC: preant.nim
CC: use_preant.nim
Hint: [Link]
Hint: operation successful (14420 lines compiled; 0.523 sec total; 15.992MiB peakmem; Debug Build) [SuccessX]
$ ./use_preant
Call Preant
Output with c.call() commented:
$ nim c use_preant.nim
Hint: used config file '.choosenim/toolchains/nim-1.0.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: use_preant [Processing]
Hint: preant [Processing]
use_preant.nim(6, 3) Error: request to generate code for .compileTime proc: call
I'm sure someone better versed in Nim will come along with a more elegant answer, and maybe someone will berate me for berating C++, and that's OK; I'll learn. Until then, I maintain that you really shouldn't go down this road to start with.