- Proposal: SE-NNNN
- Author: Brent Royal-Gordon
- Status: Draft
- Review manager: TBD
Sequence and Collection offer many special operations which access or
protocol MyNode { | |
var total: Int { get } | |
} | |
protocol MyParentNode: MyNode { | |
// Each MyParentNode-conforming type can only have certain types of child nodes | |
// (basically, type A can have B, C, or D; B can have C or D; C can have D; and D is not a MyParentNode). | |
// I want to model these limitations in the types of children permitted. | |
associatedtype Child: MyNode | |
var children: [Child] { get set } |
// Evaluates the given closure when two `Optional` instances are not `nil`, | |
// passing the unwrapped values as parameters. (Thanks, Mike Ash, Tim Vermeulen) | |
// Following the example of IEEE754 with NAN, any comparison involving | |
// .None is false | |
// | |
// See also: http://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values | |
/// Returns nil if either parameter is nil, or a tuple of the parameters | |
/// otherwise. | |
func all<T, U>(_ first: T?, _ second: U?) -> (T, U)? { |
// Untested; consider this a sketch | |
typealias HierarchicalTurnstyleState = StatusState<FunctioningTurnstyleState> | |
enum StatusState<MachineState: StateType>: StateType { | |
case Functioning(MachineState) | |
case Broken(oldState: MachineState) | |
typealias Event = StatusEvent<MachineState.Event> | |
typealias Command = MachineState.Command |
let array = Array(1...10) | |
array[.startIndex ..< 3] | |
array[.startIndex + 2 ..< .endIndex - 1] |
let array = Array(1...10) | |
array[$ ..< 3] | |
array[$ + 2 ..< $ - 1] |
//: Playground - noun: a place where people can play | |
func withSampleData(f: (inout [Int]) -> Void) { | |
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
f(&array) | |
} | |
// IncompleteRange, nil lowerBound | |
withSampleData { array in | |
array[..<6] |
struct UnsafeMutableRawPointer : Strideable, Hashable, _Pointer { | |
// Anything I don't list, I'm okay with. SE-0107 names are commented | |
// out above my alternatives. | |
// I don't like the use of `cast` for these. Yes, "cast" is its own | |
// past participle, but it's not clear that these operations are | |
// nonmutating. I prefer `casting` because it's clearer about that. | |
// | |
// func cast<T>(to: UnsafeMutablePointer<T>.Type) -> UnsafeMutablePointer<T> | |
// func cast<T>(to: UnsafePointer<T>.Type) -> UnsafePointer<T> |
Sequence and Collection offer many special operations which access or
The Sequence
and Collection
protocols offer a wide variety of APIs
which are defined to operate on, or from, one end of the sequence:
Fixed Size | Return | Return Index (1) | Subsequence without | Remove (2) | Pop (2) | Equate (1) (3) |
---|---|---|---|---|---|---|
First 1 | C.first | - | S.dropFirst() | C.removeFirst() | C.popFirst() | - |
Last 1 | C.last | - | S.dropLast() | C.removeLast() | C.popLast() | - |
First (n: Int) | S.prefix(_:) | - | S.dropFirst(_:) | C.removeFirst(_:) | - | S.starts(with:) |
Last (n: Int) | S.suffix(_:) | - | S.dropLast(_:) | C.removeLast(_:) | - | - |
**Matching A |
This proposal enhances optional binding with a new, tuple-based syntax for