Guest User

Untitled

a guest
Jul 30th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. struct User {
  2. let id: Int
  3. let name: String
  4. let email: String?
  5. }
  6.  
  7. extension User: JSONDecodable {
  8. static func create(id: Int, name: String, email: String?) -> User {
  9. return User(id: id, name: name, email: email)
  10. }
  11.  
  12. static func decode(json: JSONValue) {
  13. return check(User.create, json["id"], json["name"], json["email"])
  14. // check() calls its fn only if the required arguments are non-nil
  15. // You could readily define check() as an infix operator that takes a tuple, e.g.:
  16. // return User.create?<(json["id"], json["name"], json["email"])
  17. }
  18. }
  19.  
  20. protocol JSONDecodeable {
  21. class func decode(json: JSONValue) -> Self?
  22. }
  23.  
  24. enum JSONValue {
  25. case JSONObject([String: JSONValue])
  26. case JSONArray([JSONValue])
  27. // etc
  28.  
  29. subscript(key: String) -> Int {}
  30. subscript(key: String) -> Bool {}
  31. // etc
  32. }
  33.  
  34. func check<A, B, C, R>(fn: (A,B,C) -> R, a: A?, b: B?, c: C?) -> R? {
  35. if a == nil || b == nil || c == nil {
  36. return nil
  37. } else {
  38. return fn(a!, b!, c!)
  39. }
  40. }
  41.  
  42. func check<A, B, C, R>(fn: (A?,B,C) -> R, a: A?, b: B?, c: C?) -> R? {
  43. if b == nil || c == nil {
  44. return nil
  45. } else {
  46. return fn(a, b!, c!)
  47. }
  48. }
  49.  
  50. func check<A, B, C, R>(fn: (A,B?,C) -> R, a: A?, b: B?, c: C?) -> R? {
  51. if a == nil || c == nil {
  52. return nil
  53. } else {
  54. return fn(a!, b, c!)
  55. }
  56. }
  57.  
  58. func check<A, B, C, R>(fn: (A,B,C?) -> R, a: A?, b: B?, c: C?) -> R? {
  59. if a == nil || b == nil {
  60. return nil
  61. } else {
  62. return fn(a!, b!, c)
  63. }
  64. }
  65.  
  66. func check<A, B, C, R>(fn: (A?,B?,C) -> R, a: A?, b: B?, c: C?) -> R? {
  67. if c == nil {
  68. return nil
  69. } else {
  70. return fn(a, b, c!)
  71. }
  72. }
  73.  
  74. func check<A, B, C, R>(fn: (A?,B,C?) -> R, a: A?, b: B?, c: C?) -> R? {
  75. if b == nil {
  76. return nil
  77. } else {
  78. return fn(a, b!, c)
  79. }
  80. }
  81.  
  82. func check<A, B, C, R>(fn: (A,B?,C?) -> R, a: A?, b: B?, c: C?) -> R? {
  83. if a == nil {
  84. return nil
  85. } else {
  86. return fn(a!, b, c)
  87. }
  88. }
  89.  
  90. func check<A, B, C, R>(fn: (A?,B?,C?) -> R, a: A?, b: B?, c: C?) -> R? {
  91. return fn(a, b, c)
  92. }
  93.  
  94. // etc.
Add Comment
Please, Sign In to add comment