开括号后的参数 [英] Parameters after opening bracket

查看:108
本文介绍了开括号后的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Vapor(Swift的Web框架)的第一步.

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"
}

我不明白这里的语法.我的意思是,我正在调用app.get()方法,但是我还定义了一种将请求作为参数的函数.我知道这将导致通过/welcome URL访问的get方法并将返回"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.

推荐答案

这称为此答案中,我对闭包的各种语法糖进行了简要介绍.

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

此代码的扩展版本为:

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

这篇关于开括号后的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆