- Proposal: SE-NNNN
- Authors: Brent Royal-Gordon
- Review Manager: TBD
- Status: First draft
String interpolation is a simple and powerful feature for expressing
import Foundation | |
let feedURL = URL(string: "https://daringfireball.net/feeds/json")! | |
// MARK: Basic models | |
// We do not model the `userComment` field because it is not supposed to be used by | |
// machines. We do not model extensions because they should be ignored unless | |
// supported anyway. | |
struct Feed { |
print(""" | |
<?xml version="1.0"?> | |
<catalog> | |
\(bookTuples.map(xmlForBookTuple).joined(separator: "") | |
)</catalog> | |
""") | |
typealias ChapterTuple = (heading: String) | |
typealias BookTuple = (id: String, author: String, title: String, genre: String, price: String, chapters: [ChapterTuple]) | |
protocol Formatter { | |
associatedtype Input | |
associatedtype Output | |
func format(_ input: Input) -> Output | |
} | |
protocol CombiningFormatter: Formatter where | |
InnerFormatter.Output == CombinedFormatter.Input | |
{ |
import Foundation | |
public protocol LocalizableArgument { | |
var localizableFormat: String { get } | |
var localizableFormatArguments: [CVarArg] { get } | |
} | |
extension LocalizableArgument where Self: CVarArg { | |
public var localizableFormatArguments: [CVarArg] { | |
return [self] |
String interpolation is a simple and powerful feature for expressing
The problem with Metatype
as a keyword is that it's one level of abstraction too far up.
Think about a typical type name: Int
, UIViewController
, Record
. What you'll notice about the type name is that it's a description of the instances of that type, not a description of the type itself. You don't see types with names like IntStruct
or UIViewControllerClass
except in beginner code.
Just as UIViewController
is a class, UIViewController.Type
(to use current terminology) is a metatype. But that doesn't mean the word "metatype" belongs in its name, any more than the word "class" belongs in UIViewController
. The type name describes the instances of that type, and the instances of UIViewController.Type
are types, so Type
, not Metatype
, is the appropriate word.
That's why I say that we should use Type<T>
, not Metatype<T>
—because Metatype<T>
would have the same mistake as UIViewControllerClass
.
"hello world".withCString { cString in | |
// Horribly, `withCString` gives you a pointer to `Int8`, everything else wants | |
// `UInt8`, and the safe way to do this, using `withMemoryRebound`, wants a length. | |
// `unsafeBitCast` is technically invalid but happens to be work in this demo. | |
let workaroundCString = unsafeBitCast(cString, to: UnsafePointer<UInt8>.self) | |
let buffer = UnsafeNulTerminatedBufferPointer(start: workaroundCString) | |
buffer.count | |
print(String(buffer, encoding: UTF8.self)) | |
} |
extension Dictionary { | |
subscript(digging first: Key, rest: AnyHashable...) -> Any? { | |
return self[digging: first, rest[0..<rest.endIndex]] | |
} | |
subscript(digging first: Key, rest: ArraySlice<AnyHashable>) -> Any? { | |
let v = self[first] | |
guard let firstOfRest = rest.first else { | |
return v |
final class AssociatedValue<Value, Object: NSObject> { | |
init(_ memoryManagement: AssociatedValueMemoryManagement, ofType valueType: Value.Type, on objectType: Object.Type) { | |
self.memoryManagement = memoryManagement | |
} | |
let memoryManagement: AssociatedValueMemoryManagement | |
var key: UnsafeRawPointer { | |
return UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque()) | |
} |
Last updated: 7 October 2016 | |
All: 37/47 | |
S = Signed | |
- = Didn't sign | |
f = Foreign citizen | |
d = Deceased | |
TOS: (2/3) | |
f Kirk: William Shatner |