SQLite.swift icon indicating copy to clipboard operation
SQLite.swift copied to clipboard

Suggestion: `Value.fromDatatypeValue` should be allowed to throw

Open MrMage opened this issue 7 years ago • 0 comments

I am implementing my own serialization methods for a few objects, e.g. NSPredicate:

extension NSPredicate: Value {
	@nonobjc public static let declaredDatatype = Blob.declaredDatatype

	public static func fromDatatypeValue(_ datatypeValue: Blob) -> NSPredicate {
		return NSKeyedUnarchiver.unarchiveObject(with: Data.fromDatatypeValue(datatypeValue)) as! NSPredicate
	}

	public var datatypeValue: Blob {
		return NSKeyedArchiver.archivedData(withRootObject: self).datatypeValue
	}
}

However, that as! NSPredicate bugs me. I would rather not have my code crash in the unexpected case that I can't unwrap an NSPredicate there. Would it be possible to augment the method signature of fromDatatypeValue with a throws clause to handle such errors? Similarly, there might be cases where serialization could fail as well, so while at it I'd suggest converting datatypeValue into a throwing function too.

MrMage avatar Jan 26 '18 11:01 MrMage