Lyricdev
GitHub

LYR-SEM0143

'using' on a value that is not Closeable

error · spec 06-errors

'using let' binds a value whose type does not conform to 'Closeable' (and is no coroutine).

Explanation

using let x = …; closes x when the scope is left, through the Closeable interface. A type that merely has a method named close() does not conform: the conformance is what promises that close releases the resource and may be called on every exit path.

Declare the conformance (class Plain :: [Closeable] { … }), or use a plain let with a defer { x.close(); }.

Wrong

class Plain {
    fn close(): void { }
}

fn main(): void {
    using let p = Plain { };
    let _ = p;
}
class Plain :: [Closeable] {
    fn close(): void { }
}

fn main(): void {
    using let p = Plain { };
    let _ = p;
}