Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. // I know you meant it to be exaggerated, but I still think this is the better approach in practice:
  2.  
  3. enum ClassError: Error {
  4. case propertyNotFound(name: String)
  5. case typeMismatch(propertyName: String)
  6. }
  7.  
  8. class Foo {
  9. var properties: [(String, Any)] = []
  10. private func validateValue(_ value: Any, for property: String) throws { // Note that I pass the name just for the error; that's ok IMO
  11. // ... some logic
  12. throw ClassError.typeMismatch(propertyName: property)
  13. }
  14.  
  15. private func validateProperty(name: String, value: Any) throws {
  16. if true {
  17. throw ClassError.propertyNotFound(name: name)
  18. }
  19. try validateValue(value, for: name) // Don't need to wrap
  20. }
  21.  
  22. func validate(name: String) throws { // should only throw a ClassError
  23. for (name, value) in self.properties {
  24. try validateProperty(name: name, value: value)
  25. }
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement