[swift-evolution] [proposal] Either in the Swift Standard Library

Brent Royal-Gordon brent at architechies.com
Mon Feb 8 08:34:34 CST 2016


> I would have probably just used an Either[String, Date],  Either[enum, Date] where enum is not necessarily an error - it could be a invalid( string) , and empty type, or a date type.  It would allow me to deal with special cases or not….  

Personally, rather than use Either<String, NSDate> with its generic .Left and .Right, I would think this would give you better code:

	enum ValidatedDate {
		static let dateFormatter = ...
		
		case Invalid (String)
		case Valid (NSDate)
		
		init(_ dateString: String) {
			if let date = ValidatedDate.dateFormatter.dateFromString(dateString) {
				self = .Valid (date)
			}
			else {
				self = .Invalid (dateString)
			}
		}
	}

Now there's no question whether Left is valid or invalid.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list