cast[var int](p) += sizeof(a[0])
    
    
    Run

compiles on 1.0.8,1.2.6 and devel which, i had no idea, and does turn a `ptr 
int` into a `var int`, so you're adding to the referred value.

and yes, UncheckedArray[T] is the way to assign to an array, but it doesn't 
make such a great example because it just hides all the math.

what about offsets in structs? 
    
    
    type
      Big{.packed.} = object
        i: int64
        j: int32
        k: int8
        l: string
    var b = Big(i: 3, j: 5, k: 7, l: "nine")
    let p = b.addr
    echo b
    cast[var int64](cast[ByteAddress](p) +% offsetOf(Big, i)) += 3
    cast[var int32](cast[ByteAddress](p) +% offsetOf(Big, j)) += 4
    cast[var int8](  cast[ByteAddress](p) +% offsetOf(Big, k)) += 5
    cast[var string](cast[ByteAddress](p) +% offsetOf(Big, l)) &= "r"
    echo b
    
    
    Run

outputs: 
    
    
    (i: 3, j: 5, k: 7, l: "nine")
    (i: 6, j: 9, k: 12, l: "niner")
    
    
    Run

still a terrible example, i don't know why you'd do that but hey, you can. 

Reply via email to