[swift-users] file io in Swift 2.2 for Linux (would like to be pointed in the right direction)
Matthias Zenger
matthias at objecthub.net
Sun Jun 5 19:09:45 CDT 2016
Yes, error reporting is missing (errors are treated like EOF), but your
proposal breaks encapsulation. By not making `NSInputStream` a hidden
implementation detail, you make it possible for clients to interfere with
the caching logic (e.g. by directly invoking `read`, or prematurely closing
the steam).
== Matthias
*Matthias Zenger* matthias at objecthub.net
On Mon, Jun 6, 2016 at 1:49 AM, Brent Royal-Gordon <brent at architechies.com>
wrote:
> > Looks good, but you'll want some error handling. There's no clean way to
> integrate it with Generator because the protocol doesn't allow the
> implementation to throw, unfortunately. (I've run into the same problem
> building a Generator around a database query.) I think the best solution is
> to add a property that returns the NSStream.streamError, or a checkError()
> method that throws the current error if any, and have the caller use those
> at the end of the iteration.
>
> You can do better.
>
> extension NSInputStream {
> // I will assume you already have a byteGenerator method
> (which you can use with a for loop)
> // with a checkError() method (which throws if the
> generation terminated due to an error). However,
> // you've made these private, and will use them to
> implement this safe public API.
> public func withByteGenerator<R>(blockSize: Int = 1024,
> iterate: @noescape (NSInputStream.ByteGenerator) throws -> R) throws -> R {
> let generator = byteGenerator(blockSize: blockSize)
> let result = iterate(generator)
> try generator.checkError()
> return result
> }
> }
>
> Now you write:
>
> guard let stream = NSInputStream(fileAtPath: path) else {
> …
> }
> try stream.withByteGenerator {
> // XXX This would all be more complicated if
> CharacterGenerator can throw, too.
> for (i, line) in LineGenerator(source:
> CharacterGenerator(source: $0, decoder: UTF8())).enumerate() {
> print("\(i+1): \(line)")
> }
> }
>
> (I'm assuming that these generators take their almost-automatic Sequence
> conformance, of course.)
>
> --
> Brent Royal-Gordon
> Architechies
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160606/9a1350f2/attachment.html>
More information about the swift-users
mailing list