Lyricdev
GitHub

LYR-SEM0174

a closeable iterator's close() throws more than its Error allows

error ยท spec 12-stdlib

A type that is both an Iterator and Closeable declares a close() that throws a type its Error (the set its next() may throw) does not cover.

Explanation

A for loop over an iterator that is Closeable closes it when the loop ends, and the one clause that covers the loop's next() calls has to cover that close() too. So a closeable iterator's close() throws only what its Error allows; with no type Error, that is nothing (never).

Let close() throw nothing, or declare type Error to cover what it throws and let next() share that set.

Wrong

class Broken :: [Error] {
    fn message(): string { return "broken"; }
}

struct Empty :: [Iterator, Closeable] {
    type Item = int;

    mut fn next(): ?int {
        return null;
    }

    fn close(): void throws Broken {
    }
}

fn main(): void {
}
struct Empty :: [Iterator, Closeable] {
    type Item = int;

    mut fn next(): ?int {
        return null;
    }

    fn close(): void {
    }
}

fn main(): void {
}