So this is admittedly wonky, but map is another option:
guard error.map({print($0)}) == nil else {
print("Guarded")
return
}
Or with your original example:
class Network {
let session: URLSession = .shared
func callServer(request: URLRequest, completion: @escaping (Result<Data,Error>) -> Void) {
let task = session.dataTask(with: request) { (data, response, error) in
guard error.map({
completion(.failure($0))
print("ERROR: \($0)")
return Void.self
}) == nil else {
return
}
// The rest of your function
}
}
}
There are usually plenty of ways to work around the issue (I usually use switch) but it does seem natural for something like this to exist. Personally I would prefer to keep guard with it's current semantics. It can already be a bit confusing to reason about. Adding a case where a let could be available in the typical scope or inside else seems problematic.
How about escape?
escape if let error = error {
// return is required
return
}