I am trying to implement a small future (promises) library with Swift 3 inspired by this talk here is my implmentation :
public enum Result<T, E: Error> {
case Success(T)
case Error(E)
}
public struct Future<T, E: Error> {
public typealias ResultType = Result<T, E>
public typealias Completion = (ResultType) -> Void
public typealias AsyncOperation = (Completion) -> Void
private let operation: AsyncOperation
public init(result: ResultType) {
self.init(operation: { completion in
completion(result)
})
}
public init(value: T) {
self.init(result: .Success(value))
}
public init(error: E) {
self.init(result: .Error(error))
}
public init(operation: #escaping (Completion) -> Void) {
self.operation = operation
}
public func start(completion: Completion) {
self.operation() { result in
completion(result)
}
}
}
//: ### Error handeling
enum UserInfoErrorDomain: Error {
case UserDoesNotExist
case UserRequestFailure
case NetworkRequestFailure
}
and here is my usage:
func downloadFile(URL: NSURL) -> Future<NSData, UserInfoErrorDomain> {
return Future(operation: { completion in
DispatchQueue.main.async( execute: {
print("Async2")
let result: Result<NSData, UserInfoErrorDomain>
if let data = NSData(contentsOf: URL as URL) {
result = Result.Success(data)
}
else {
result = Result.Error(.NetworkRequestFailure)
}
completion(result) // ERROR here Closure use of non-escaping parameter 'completion' may allow it to escape
})
})
}
but I get in the line of completion(result) and error of Closure use of non-escaping parameter 'completion' may allow it to escape
But the closure is already marked as #escaping in the method public init(operation: #escaping (Completion) -> Void) but maybe because it's a closure that takes a closure as argument and returns void needs another annotation, so to do that in Swift 3 because it seems that the code used to work in Swift 2
[...] but maybe because it's a closure that takes a closure as argument and returns void needs another annotation [...]
You're right. Completion is of type (ResultType) -> Void, which, as it's a parameter to your AsyncOperation function type, means that it's non-escaping by default – meaning that you cannot capture your completion parameter in an escaping closure (such as one passed to DispatchQueue.main.async).
Therefore you need to annotate Completion as #escaping:
public typealias AsyncOperation = (#escaping Completion) -> Void
and you'll want your init(operation:) and start(completion:) functions to look like this:
public init(operation: #escaping AsyncOperation) {
self.operation = operation
}
// the completion: parameter needs to be escaping as it's going to be called after
// an async operation has completed.
public func start(completion: #escaping Completion) {
self.operation { result in
completion(result)
}
}
Related
I'm trying to create a class that can save different closures (or methods) with an argument of a specific subtype of Decodable that should be called later. This way I can predefine what actions, or methods, can be called on that class in response to some input. For example, the line addCallback(setOption(_:), SetOptionRequest.self) should result in the subsequent call to try! performCallback("setOption", JSONEncoder().encode(SetOptionRequest()) to call setOption(data) where the argument data has type SetOptionRequest.
Here is the code I have so far (I took the bit about DecodableWrapper from here). The problem is that at runtime the cast callback.callback as! (ActionRequest) throws -> Void fails, since the type of the closure is not (ActionRequest) throws -> Void but (SetOptionRequest) throws -> Void. But I have no idea if and how I can cast the closure back to its original type. I considered using Selectors but I would like to keep the compile-time check that I'm binding methods with their correct argument type.
struct DecodableWrapper: Decodable {
static var baseType: ActionRequest.Type!
var base: ActionRequest
init(from decoder: Decoder) throws {
self.base = try DecodableWrapper.baseType.init(from: decoder)
}
}
open class Server {
private var actionCallbacks = [String: (callback: Any, dataType: ActionRequest.Type)]()
open func setup() {
addCallback(setOption, action: SetOptionRequestResponse.self)
}
public func addCallback<T: ActionRequest>(_ callback: #escaping (_ data: T) throws -> Void, action: T.Type) {
actionCallbacks[T.action] = (callback, T.self)
}
private func performCallback(action: String, data: Data) throws {
let callback = actionCallbacks[action]!
DecodableWrapper.baseType = callback.dataType
let data = try! JSONDecoder().decode(DecodableWrapper.self, from: data).base
try (callback.callback as! (ActionRequest) throws -> Void)(data)
}
private func setOption(_ data: SetOptionRequest) {
}
}
protocol ActionRequest {
static var action: String
}
struct Request: SetOptionRequest {
}
Thanks to this article, I came up with a solution. The trick is to store a custom closure that always accepts the same argument type (in this case Data), does whathever needs to be done with the generic type T and then calls the nested closure.
open class Server {
private var actionCallbacks = [String: (Data) throws -> Void]()
open func setup() {
addCallback(setOption)
}
public func addCallback<T: ActionRequest>(_ callback: #escaping (_ data: T) throws -> Void) {
actionCallbacks[T.action] = { data in
let data = try JSONDecoder().decode(T.self, from: data)
try callback(data)
}
}
private func performCallback(action: String, data: Data) throws {
if let callback = actionCallbacks[action] {
try callback(data)
}
}
private func setOption(_ data: SetOptionRequest) {
}
}
public protocol ActionRequest: Codable {
static var action: String { get }
}
struct SetOptionRequest: ActionRequest {
static var action = "setOption"
}
I'd like to provide Combine counterparts to completion closures which is becoming very cumbersome. Is there a shorter way or extension that can convert the following:
extension MyType {
func send(with request: URLRequest, completion: #escaping (Result<MyResponse, MyError>) -> Void) {
// Logic
}
func send(with request: URLRequest) -> Future<MyResponse, MyError> {
Future<MyResponse, MyError> { promise in
send(with: request) { result in
switch result {
case let .success(response):
promise(.success(response))
case let .failure(error):
promise(.failure(error))
}
}
}
}
}
The Future method is just a wrapper to the completion closure method. I was hoping to do at least something like this:
Future<MyResponse, MyError> { send(with:request, completion: $0) }
Is there a more elegant way to do this since this will be applied in a lot of places in my library.
Note that the completion parameter of the first send overload has the type:
Result<MyResponse, MyError>) -> Void
Which is exactly the same type as promise, which is
Future<MyResponse, MyError>.Promise
Promise is just a type alias for (Result<Output, Failure>) -> Void.
So you can just do:
Future<MyResponse, MyError> { promise in
send(with: request, completion: promise)
}
I'm trying to wrap my head around generic type constraints in Swift. Here is my starting point:
class Promise<T> {
func resolve(_ block:#escaping (T) ->Void) {}
func fulfill(_ result:T) {}
}
A Promise is just that, something that can be fulfilled in the future. This becomes very useful when it is used with Swift's Result type when handing results back from a background queue to the main queue:
let promise = Promise<Result<String, Error>>()
promise.fulfill(.success("Hello"))
promise.fulfill(.failure(NSError()))
Now I would like to add an extension to all Promise instances that use Result to add these helper methods:
extension Promise where T == Result<X, Error> {
⬆︎ Here's the problem ⚡️
func failure(_ error:Error) {
fulfill(.failure(error))
}
func success(_ result:X) {
fulfill(.success(result))
}
}
// Shorter:
let promise = Promise<Result<String, Error>>()
promise.success("Hello")
promise.failure(NSError())
Only problem is that the above code does not compile, because X is not defined. What I want to express is this:
Extend Promise when its generic type T is of type Result<X,Z> where X can be anything and Z must be of type Error → Result<*, Error>. Is this possible?
What you want is possible, it's just that the syntax is slightly verbose. You can't put the where constraint on the extension. You have to put it on each method.
extension Promise {
func failure<U>(_ error: Error) where T == Result<U, Error> {
fulfill(.failure(error))
}
func success<U>(_ result: U) where T == Result<U, Error> {
fulfill(.success(result))
}
}
You can also do it with a protocol, but for enums I find this very klunky, because enum cases can't be treated as conforming methods.
protocol ResultType {
associatedtype Success
associatedtype Failure: Error
static func makeFailure(_: Failure) -> Self
static func makeSuccess(_: Success) -> Self
}
extension Result: ResultType {
static func makeFailure(_ failure: Failure) -> Result<Success, Failure> { .failure(failure) }
static func makeSuccess(_ success: Success) -> Result<Success, Failure> { .success(success) }
}
extension Promise where T: ResultType {
func failure(_ error: T.Failure) {
fulfill(T.makeFailure(error))
}
func success(_ result: T.Success) {
fulfill(T.makeSuccess(result))
}
}
Is Map function of Optional in Swift just used to transform?
If I want do something just Optional has some value, Can I Use map function? if not, why?
According to apples examples, we used map like this
let possibleNumber: Int? = Int("42")
possibleNumber.map { $0 * $0 }
Can I use it like this? : (If it's not proper, how to explain it)
func setImage(with data: Data?) {
data.flatMap { UIImage(data: $0) }
.map { imageView.image = $0 }
}
Furthermore map function should return a value, but why this function does not have any warning about not used of result ( such as result of call map{...} is unused ) ?
You certainly can do it, it's just not very conventional. When people see map, they have a pre-conceived expectation that it'll be doing a transformation. You're violating that expectation, but there's nothing technically "wrong" about it.
Personally, I prefer using this extension:
extension Optional {
/// An enum used to ensure that `ifNone` is never called before `ifSome`.
enum IfSomeResult {
case some
case none
func ifNone(_ closure: () throws -> Void) rethrows -> Void {
switch self {
case .some: return
case .none: try _ = closure()
}
}
}
#discardableResult
func ifSome(then closure: (Wrapped) throws -> Void) rethrows -> IfSomeResult {
if let wrapped = self {
try _ = closure(wrapped)
return IfSomeResult.some
}
else {
return IfSomeResult.none
}
}
func ifNone(then closure: () throws -> Void) rethrows -> Void {
if case nil = self { try _ = closure() }
}
}
And writing code like:
data.flatMap { UIImage(data: $0) }
.ifSome { imageView.image = $0 }
Why doesn't it warn about an unused value?
The closure is inferred to return Void (the empty tuple type, whose only value is the empty tuple, ()). The compiler never emits warnings about Void values being unused`. Here's an example:
Optional(123).map { _ in () }
Hi I am trying to understand the following code from Alamofire. How can you initialise a struct with "{}" I know that you can call a closure with Trailing Closures. I know I am totally missing something, but what?
extension Request {
public func responseObject<T: ResponseObjectSerializable>(completionHandler: Response<T, NSError> -> Void) -> Self {
let responseSerializer = ResponseSerializer<T, NSError> { // What is this?
request, response, data, error in
guard error == nil else { return .Failure(error!) }
let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, data, error)
switch result {
case .Success(let value):
if let
response = response,
responseObject = T(response: response, representation: value)
{
return .Success(responseObject)
} else {
let failureReason = "JSON could not be serialized into response object: \(value)"
let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
return .Failure(error)
}
case .Failure(let error):
return .Failure(error)
}
}
return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}
}
The struct ResponseSerializer from Alamofire
public struct ResponseSerializer<Value, Error: ErrorType>: ResponseSerializerType {
/// The type of serialized object to be created by this `ResponseSerializer`.
public typealias SerializedObject = Value
/// The type of error to be created by this `ResponseSerializer` if serialization fails.
public typealias ErrorObject = Error
/**
A closure used by response handlers that takes a request, response, data and error and returns a result.
*/
public var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>
/**
Initializes the `ResponseSerializer` instance with the given serialize response closure.
- parameter serializeResponse: The closure used to serialize the response.
- returns: The new generic response serializer instance.
*/
public init(serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>) {
self.serializeResponse = serializeResponse
}
}
Your question can be greatly pared down (and should have been). Here is the relevant declaration of ResponseSerializer:
public struct ResponseSerializer<Value, Error: ErrorType>: ResponseSerializerType {
public init(serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>) {
self.serializeResponse = serializeResponse
}
}
So this initializer, init(serializeResponse:), takes one parameter — a function taking four parameters and returning one parameter (of the specified types).
Thus, we can initialize like this:
func f (request:NSURLRequest?, response:NSHTTPURLResponse?, data:NSData?, error:NSError?) -> Result<Value, Error>) {
guard error == nil else { return .Failure(error!)
}
let responseSerializer = ResponseSerializer<T, NSError>(serializeResponse:f)
However, this can be condensed. We don't really need the function f for anything else, so we can supply it as an anonymous function; it doesn't need a name or a full declaration. Moreover, there is a "shortcut" rule for anonymous functions, that if an anonymous function is the last parameter to a function, it can be provided literally after the function's closing parentheses, with the parameter name omitted. And if the function takes no other parameters, its parentheses can be omitted altogether.
Well, this init is exactly such a function — it takes a function as its last (and only) parameter — so that is exactly what the code in question does:
let responseSerializer = ResponseSerializer<T, NSError> {
request, response, data, error in
guard error == nil else { return .Failure(error!)
}
If I read it all correctly the code above has a pattern similar to this:
// just a something
struct Blah {
var stuffs : (message:String) -> Void
init(closure:(message:String) -> Void) {
self.stuffs = closure
}
}
// an extension because the code above is also in an extension, but not needed at all
extension Blah {
// a function with a closure that also returns an instance of Self
func spawnChild(closure:(message:String) -> Void) -> Blah {
return Blah(closure: closure) // closure is passed to spawn
}
}
Tests :
let alpha = Blah { (message) -> Void in
print("alpha",message)
}
let beta = alpha.spawnChild { (message) -> Void in
print("beta", message)
}
alpha.stuffs(message: "parrent") // alpha parent
beta.stuffs(message: "spawn") // beta spawn
Remember that the trailing closure is just syntactic sugar for an input parameter that is a closure. So anything that takes input parameters can have a trailing closure.