You can use the `lent` keyword for getting a immutable reference, and you can 
override the `=copy` hook to see whether you're copying. Nim is intelligent 
enough with the lent annotation to copy if you call it to a `var` and then 
mutate it.

So an example of this is as follows:
    
    
    import random
    
    type Big = object
        c: array[10000, int]
    
    proc `=copy`(a: var Big, b: Big) =
      echo "We Copied"
    
    # needs mutable seq[Big]
    func get0(s: var seq[Big], i: int): var Big =
        s[i]
    
    # uses expensive copy
    func get1(s: seq[Big], i: int): lent Big = s[i]
    
    # needs "internal" function, unsafeAddr, and is verbose
    func getInternal(s: seq[Big], i: int): ptr Big =
        unsafeAddr s[i]
    template get2(s: seq[Big], i: int): Big =
        s.getInternal(i)[]
    
    var s: seq[Big]
    s.setLen(100)
    
    echo s.get0(rand(0..99)).c[0]
    echo s.get1(rand(0..99)).c[0]
    echo s.get2(rand(0..99)).c[0]
    
    
    Run

To your "sorry for asking so many questions" you could go to the [realtime 
chats](https://nim-lang.org/community.html) and ask simple questions.

Reply via email to