You can do what you want at compile time with a macro like so:
import macros
macro msizeof(obj: typedesc): untyped =
let ty = obj.getType()
var sym: NimNode
if ty[1].kind == nnkBracketExpr:
# This is a ref object
sym = ty[1][1]
else:
# This is a normal object
sym = ty[1]
result = quote do:
sizeof(`sym`)
type
MyObject = object
a : int
b : int
c : int
MyRefObject = ref object
a : int
b : int
c : int
echo msizeof(MyObject) # Prints 24
echo msizeof(MyRefObject) # Also prints 24
Run
- sizeof a ref object type vitreo12
- Re: sizeof a ref object type roel
- Re: sizeof a ref object type vitreo12
- Re: sizeof a ref object type mratsim
- Re: sizeof a ref object type roel
- Re: sizeof a ref object type jyapayne
