You can simulate move semantic by using shallowCopy. 
    
    
    proc worker(): seq[int] =
        var arr = @[1, 2, 3]
        # do some heavy work here
        
        # deep copy is made here
        return arr
        # or
        shallowCopy(result, arr) # move arr to result without copying
        return # not necessary at end of function (implicit)
        
        # you can also use result var from the beginning avoid copies.
    
    

if you don't like shallowCopy syntax you can use a template to emulate shallow 
assignment semantics.
    
    
    template `:=` (x: var seq[T]; v: seq[T]) =
        shallowCopy(x, v)
    
    # used like this
    var s0 = @[1,2,3]
    let s1 = s0 # no copy is made since s1 cannot be modified
    var s2 = s0 # a copy is made here (unless s0 was marked as shallow)
    var s3: seq[int]
    s3 := s0 # custom template use shallowCopy
    

there is also the swap proc which swap two variables without any deep copy.

You can laso mess with assignment semantics for types

[https://nim-lang.org/docs/manual.html#type-bound-operations-operator](https://nim-lang.org/docs/manual.html#type-bound-operations-operator)

Reply via email to