1.dsl (domain specific language) 赋予开发者一个特殊能力,编写出脱离原语言的语法结构,通过俩个示例来展示它的魅力
示例1.
dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
}
代码如下:

fun dependencies(block:Dependency.()->Unit):MutableList<String>{
val item = Dependency()
item.block()
return item.libraries
}
扩展lamda语法,此时的block拥有Dependency类的上下文,所以可以在内部可以调用implement函数
示例二:

执行代码结果:

kotlin的扩展函数功能太强大,这也是kotlin的魅力
855

被折叠的 条评论
为什么被折叠?



