Parameters after opening bracket

点点圈 提交于 2019-11-27 07:16:40

问题


I am doing my first steps in Vapor, the web framework for Swift.

The first piece of code that called my attention was this:

app.get("welcome") { request in 
    return "Hello"
}

I don't understand the syntax here. I mean, I'm calling app.get() method, but I'm also defining some kind of function where request is a parameter. I know that this will result in a get method accessible by a /welcome URL and will return "Hello". What is not clear for me is how this piece of code works and how the compiler interprets it.


回答1:


This is called trailing closure syntax.

I give a nice rundown of the various syntactic sugars of closures in this answer.

The expanded version of this code would be:

app.get("welcome", { (request: Request) throws -> ResponseRepresentable in 
    return "Hello"
})


来源:https://stackoverflow.com/questions/40592541/parameters-after-opening-bracket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!