kotlin多方法(?)语法



我正在研究Spring安全性,并在官方Spring文档中发现了一段我无法解释的代码片段,Idea显示它语法不正确

@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeRequests {
authorize(anyRequest, authenticated)
}
formLogin { }
httpBasic { }
}
return http.build()
}

https://docs.spring.io/spring-security/reference/servlet/configuration/kotlin.html

谁能给我一个提示,如何解析?我所知道的最接近Kotlin语法是"多重方法"语句,但它是不同的。

此外,术语anyRequest和authenticated看起来未声明/未绑定…

https://kotlinlang.org/docs/idioms.html call-multiple-methods-on-an-object-instance-with

这里有两件事可能涉及到。

调用带有lambda参数的函数

如果函数的最后一个参数允许传递lambda函数,则使用lambda{ }调用函数/构造函数有效。

//normal lambda as parameter
function({...})
//last argument lambda outside of parentheses
function() {...}
//no parentheses for "better" readability
function {...}

功能接口Kotlin允许单函数接口,也称为单抽象方法。去看医生。有了这个,你可以声明一个函数而不给出实现,这对库很有用。

最新更新