type
Obj* = object
m: int
proc m*(o: Obj): int = o.m
proc `m=`*(o: var Obj, v: int) = o.m = v
proc `+=` for m ?
add
proc m*(o: var Obj): var int = o.m
type
Obj = object
m: cint
proc `m`*(o: var Obj): var int = int(o.m) # Error
I don't think it can be done in that case. Well, there's an unsafe way of doing it if you know that the two types are binary compatible and of the same size.
type
Obj = object
m: cint
static:
doAssert(sizeof(cint) == sizeof(int32))
proc `m`*(o: var Obj): var int32 =
cast[ptr int32](addr o.m)[]
var obj: Obj
obj.m += 5
echo obj.m # 5
buuut this is extremely situational and I wouldn't recommend it at all. You're best off just making shorthand procedures that act on the object itself.
type
Obj = object
m: cint
proc incM*(o: var Obj, n: int) =
o.m = cint(int(o.m) + n)
proc decM*(o: var Obj, n: int) =
o.m = cint(int(o.m) - n)
var obj: Obj
obj.incM(5)
echo obj.m # 5
I would like to preserve the '+=' and '-=' operators for structure members.
What does that mean.