[swift-users] Closure typing changed in Swift 3
    Rick Mann 
    rmann at latencyzero.com
       
    Fri Sep  9 16:34:46 CDT 2016
    
    
  
I have some code that implements an HTTP server. You use it like this:
server["/some/path"] =
{ inReq in
    return .ok(.json(["key" : "value"]))
}
".ok" is a case in the HttpResponse enum.
The subscript on "server" above looks like this:
class HttpServer {
    typealias Handler = (HttpRequest) -> HttpResponse
    subscript (path: String) -> Handler?
    {
        get { return nil }
        set ( newValue )
        {
            ...store in dictionary of path:Handler...
        }
    }
}
Unfortunately, in Swift 3, I get "Cannot assign value of type '(_) -> _' to type 'HttpServer.Handler?'"
It seems the type inference is working differently? I tried { (inReq: HttpRequest) in ... }, but I got the same error with a slightly different type signature.
Can anyone tell me what's changed?
TIA,
-- 
Rick Mann
rmann at latencyzero.com
    
    
More information about the swift-users
mailing list