the “how” • Express logic, not control flow • Abstracts between description and implementation • Code that takes 0 mental cost to understand • What you see is what you get
// reset file writing.write("new text".data(using: .utf8)!) // Update var data = updating.readDataToEndOfFile() var string = String(data: data, encoding: .utf8)! string += " with more new text" let newData = string.data(using: .utf8)! updating.seek(toFileOffset: 0) updating.write(newData)
for reading, writing, or updating • Runtime error when read-only handle attempts to write • No static enforcement • Reading from write-only handle silently errors by returning 0-length data
the handle to the type declaration as a “trait”. A trait here is just basically a type that wraps a static value. • Read this trait and performed custom logic. • Implemented protocols for these traits to allow scalable where-clauses.
all! • No code duplication for “seek” function • No code duplication for read/write for update access mode • Can be subclassed without said code duplication
read32: TextFileHandle<UTF32, ReadAccess> var write8: TextFileHandle<UTF8, WriteAccess> var write16: TextFileHandle<UTF16, WriteAccess> var write32: TextFileHandle<UTF32, WriteAccess> var update8: TextFileHandle<UTF8, UpdateAccess> var update16: TextFileHandle<UTF16, UpdateAccess> var update32: TextFileHandle<UTF32, UpdateAccess>
protocol that specifies a visual effect that can be injected into a UIView subclass) • Cosmetic annotations, for serving no real purpose but to write self-documenting type declarations