Skip to content

Suggestion: Value.fromDatatypeValue should be allowed to throw #778

Open
@MrMage

Description

@MrMage

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions