Skip to content

Instantly share code, notes, and snippets.

@andymatuschak
Created December 28, 2014 18:17
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save andymatuschak/2b311461caf740f5726f to your computer and use it in GitHub Desktop.
Save andymatuschak/2b311461caf740f5726f to your computer and use it in GitHub Desktop.
A pragmatic and intentionally non-abstract solution to JSON decoding / initialization that doesn't require learning about five new operators.
struct User {
let id: Int
let name: String
let email: String?
}
extension User: JSONDecodable {
static func create(id: Int, name: String, email: String?) -> User {
return User(id: id, name: name, email: email)
}
static func decode(json: JSONValue) {
return check(User.create, json["id"], json["name"], json["email"])
// check() calls its fn only if the required arguments are non-nil
// You could readily define check() as an infix operator that takes a tuple, e.g.:
// return User.create?<(json["id"], json["name"], json["email"])
}
}
protocol JSONDecodeable {
class func decode(json: JSONValue) -> Self?
}
enum JSONValue {
case JSONObject([String: JSONValue])
case JSONArray([JSONValue])
// etc
subscript(key: String) -> Int {}
subscript(key: String) -> Bool {}
// etc
}
func check<A, B, C, R>(fn: (A,B,C) -> R, a: A?, b: B?, c: C?) -> R? {
if a == nil || b == nil || c == nil {
return nil
} else {
return fn(a!, b!, c!)
}
}
func check<A, B, C, R>(fn: (A?,B,C) -> R, a: A?, b: B?, c: C?) -> R? {
if b == nil || c == nil {
return nil
} else {
return fn(a, b!, c!)
}
}
func check<A, B, C, R>(fn: (A,B?,C) -> R, a: A?, b: B?, c: C?) -> R? {
if a == nil || c == nil {
return nil
} else {
return fn(a!, b, c!)
}
}
func check<A, B, C, R>(fn: (A,B,C?) -> R, a: A?, b: B?, c: C?) -> R? {
if a == nil || b == nil {
return nil
} else {
return fn(a!, b!, c)
}
}
func check<A, B, C, R>(fn: (A?,B?,C) -> R, a: A?, b: B?, c: C?) -> R? {
if c == nil {
return nil
} else {
return fn(a, b, c!)
}
}
func check<A, B, C, R>(fn: (A?,B,C?) -> R, a: A?, b: B?, c: C?) -> R? {
if b == nil {
return nil
} else {
return fn(a, b!, c)
}
}
func check<A, B, C, R>(fn: (A,B?,C?) -> R, a: A?, b: B?, c: C?) -> R? {
if a == nil {
return nil
} else {
return fn(a!, b, c)
}
}
func check<A, B, C, R>(fn: (A?,B?,C?) -> R, a: A?, b: B?, c: C?) -> R? {
return fn(a, b, c)
}
// etc.
@JaviSoto
Copy link

The problem with code that takes an NSErrorPointer and returns an optional is that the compiler can’t enforce the combination of the values makes sense. There are two fields, and therefore 4 cases, but only 2 of them make sense (value present, error nil, or value nil and error present), the other two cases are bogus (both value and error are nil, or both value and error are present), which could cause weirdness at runtime. This is precisely the benefit that the improved Swift type system can bring: being able to reason about code knowing that only correct cases are possible at runtime.

We can implement flatMap on Either (Result) in the example above, and we would end up with a very simple solution:

enum Result<T> {
    case Value(T)
    case Error(NSError)
}

extension Result {
    func flatMap<U>(f: T -> Result<U>) -> Result<U> {
        switch (self) {
        case let Value(value):
            return f(value)
        case let Error(error):
            return Result<U>.Error(error)
        }
    }
}

func pagesFromOpenSearchData(data: NSData) -> Result<[Page]> {
    let pages = asJSON(data)
        .flatMap { asJSONArray($0) }
        .flatMap { atIndex($0, 1) }
        .flatMap { asStringList($0) }
        .flatMap { asPages($0) }

    return pages
}

@adriantofan
Copy link

Hello,

Just a quick note to tell you that for checks up to n parameters there are 2^1 + 2^2 + 2^3+....+2^(n-1)+2^n distinct combinations. I will just say(without going in to the details about how I found out) that the current Swift compiler is not really up to the task.

I believe that it might be a good approach for very practical reasons but unfortunately it is un-feasable (today?).

What might work is to use check functions up to 4-5 parameters and handle the rest manually.

What is interesting to me is the very declarative style and lack of voodoo to make the mapping work. It looks to me really easy to debug. Even more interesting is the error handling version which could produce very precise error messages so one doesn't need anymore to trace the code in order to understand errors.

Adrian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment