> Maybe you can use a wrapper type for the hot path which has an overriden = 
> that is disabled.

Sounds like a nice workaround for now, thanks for the advice !

> But how to offer the feature otherwise... I don't know. An RFC is welcome.

So before i start writing an RFC, i would like to present my understanding (and 
proposal) so you can smash me here before i start writing a silly RFC :D

Consider this trivial example:
    
    
    type
        X = object
          s: string
    
    proc `=copy`(x: var X, y: X): void =
        x.s = "copy of " & y.s
    
    proc consume(x: sink X) =
        echo "Consumed: ", x.s
    
    var x = X(s: "abcdefg")
    
    consume(move(x))
    
    echo "Unconsumed: ", x.s # < Could the compiler hint i'm using after move ?
    
    
    Run

This prints this: 
    
    
    Consumed: abcdefg
    Unconsumed:
    
    
    Run

But i would like to enable a compiler hint, if possible

Btw i'm still trying to understand how the sink works (sorry i'm pretty new to 
the language, coming from C++). Consider this slight variation:
    
    
    type
        X = object
          s: string
    
    proc `=copy`(x: var X, y: X) =
        x.s = "copy of " & y.s
    
    proc `=sink`(x: var X, y: X) =
        `=destroy`(x)
        wasMoved(x)
        x.s = "moved " & y.s
    
    proc consume(x: sink X) =
        echo "Consumed: ", x.s
    
    var x = X(s: "abcdefg")
    
    consume(x)
    
    
    Run

This surprisingly prints: 
    
    
    Consumed: copy of abcdefg
    
    
    Run

the compiler seems unable to prove x is never read after the call to the proc 
with sinked argument. To confirm this, i tried this other snippet, and it 
doesn't work like i would expect:
    
    
    type
        X = object
          s: string
    
    proc `=copy`(x: var X, y: X) {.error.}
    
    proc `=sink`(x: var X, y: X) =
        `=destroy`(x)
        wasMoved(x)
        x.s = "moved " & y.s
    
    proc consume(x: sink X) =
        echo "Consumed: ", x.s
    
    var x = X(s: "abcdefg")
    
    consume(x)
    
    
    
    Run

Produces: 
    
    
    Error: '=copy' is not available for type <X>; requires a copy because it's 
not the last read of 'x'; routine: test
    
    
    Run

Reply via email to