Actors and AsyncStream

I'm just starting to experiment with actors and I ran across an issue that I don't completely understand. I have trimmed down the example:

import Foundation

public actor MyDocument {
    internal let file: FileHandle
    
    internal var lines: AsyncLineSequence<FileHandle.AsyncBytes>.AsyncIterator
    
    public init(forReadingAtPath path: String) throws {
        guard let file = FileHandle(forReadingAtPath: path) else {
            throw MyDocumentError.fileDoesNotExist
        }
        self.file = file
        lines = self.file.bytes.lines.makeAsyncIterator()
    }
    
    internal func getLine() async throws -> String? {
        // Error: Cannot call mutating async function 'next()' on actor-isolated property 'lines'
        let line = try await lines.next()
        return line
    }
    
    internal func getLines() async throws -> [String] {
        var buffer: [String] = []
        for try await line in file.bytes.lines {
            buffer.append(line)
        }
        return buffer
    }
}

public enum MyDocumentError: Error {
    case fileDoesNotExist
}

I do not understand why the compiler gives the error I show above. Can someone please provide some guidance regarding this?

What I find most confusing is the fact that I can use file.bytes.lines in getLines, but cannot access the lines property from getLine.

Terms of Service

Privacy Policy

Cookie Policy