[swift-evolution] [Review] SE-0109: Remove the Boolean protocol

Brent Royal-Gordon brent at architechies.com
Wed Jun 29 22:55:07 CDT 2016


> https://github.com/apple/swift-evolution/blob/master/proposals/0109-remove-boolean.md

> 	• What is your evaluation of the proposal?

+1. I hope we eventually get this functionality back through a more flexible mechanism—either subtyping Bool or a protocol for creating custom conditional constructs[1]—but I don't think `Boolean` is really carrying its weight at this point.

> 	• Is the problem being addressed significant enough to warrant a change to Swift?

Yes. We're trying to get rid of this kind of thing now.

> 	• Does this proposal fit well with the feel and direction of Swift?

See previous.

> 	• If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

The only language I've used 

> 	• How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

Quick reading.



[1] For instance, if we have a pattern matching protocol like this:

	// Allows for `if case .foo(let bar, let baz) = quux`-style pattern matching.
	protocol PatternProtocol {
		associatedtype Value
		associatedtype Destructured = Void
		
		// Returns nil for a failed match, or the destructured values for a 
		// successful match.
		func match(for value: Value) -> Destructured?
		
		// One could also imagine a `func init(values...: Destructured) -> Value` here
	}
	
	// Enum cases would have a pattern as one of their "aspects", as though 
	// they had things like:
	extension Optional {
		static let none: Pattern.None
		static let some: Pattern.Some
		
		enum Pattern {
			struct None: PatternProtocol {
				typealias Value = Optional<Wrapped>
				func match(for value: Value) -> Void? { … }
			}
			struct Some: PatternProtocol {
				typealias Value = Optional<Wrapped>
				typealias Destructured = Wrapped
				func match(for value: Value) -> Void? { … }
			}
		}	
	}
	
	// You could write your own patterns for other types:
	extension URL {
		static let http = SchemePattern(scheme: "http")
		static let https = SchemePattern(scheme: "https")
		static let mailto = SchemePattern(scheme: "mailto")
		
		struct SchemePattern: PatternProtocol {
			associatedtype Value = URL
			associatedtype Destructured = (host: String, path: String)
			
			var scheme: String
			
			func match(for value: Value) -> Destructured? {
				guard value.scheme == scheme else { return nil }
				return (value.host, value.path)
			}
		}
	}

Then we could offer a `ConditionalConvertible` protocol which would open up `if let` and plain `if` to other types:
	
	// Allows for `if let (bar, baz) = quux` and, if UnwrappedValues is Void, `if quux`.
	protocol ConditionalConvertible {
		associatedtype Conditional: PatternProtocol where ConditionalPattern.Value == Self
		static var conditional: Conditional
	}
	
	extension Optional: ConditionalConvertible {
		typealias Conditional = Pattern.Some
		static let conditional = some
	}
	
	extension Bool: ConditionalConvertible {
		struct Conditional: PatternProtocol {
			typealias Value = Bool
			
			func match(for value: Value) -> Void? {
				// equivalent to...
				return value ? () : nil
			}
		}
		static let conditional = Conditional()
	}

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list