KT 高阶函数Lambda

早上看了一篇推文,https://mp.weixin.qq.com/s/UleyW3kCxuoNeXg5RhdvyQ
例中写到

class ToolbarConfig(
  var title: String? = null,
  var navBtnType: NavBtnType = NavBtnType.ICON,
  @DrawableRes var rightIcon: Int? = null,
  var rightText: String? = null,
  var onRightClickListener = View.OnClickListener? = null
)

enum class NavBtnType {
  ICON, NONE
}

fun Activity.setToolbar(block: ToolbarConfig.() -> Unit) {
  // 根据 ActionBar 原理动态添加标题栏
}

setToolbar {
  title = "title"
  navBtnType = NavBtnType.NONE
}

看完迷茫了,扩展函数接受一个 ToolBarConfig 的任意函数,不接受参数,无返回值的函数体,为什么在 setToolBar中用的却是 (title=“title” navBtnType=NavBtnType.NONE)。一直理解不了,刚开始甚至以为是连续调用了 ToolBarConfig 属性的get/set隐藏方法,后来突然发现不对。应该是 使用了一个匿名函数,匿名函数没有 参数 没有返回值,(如果有应该是这样)
错误代码

setToolbar {   it->
  title = "title"
  navBtnType = NavBtnType.NONE
return xxxx
}

setToolBar大括号中 为匿名函数的 函数体。##但是我不理解。##,高阶函数支持这样做的吗,难道不是应该是限定于ToolbarConfig::class 中的方法吗。我不理解。。。。。

如果有错误,请大佬指正。##

你可能感兴趣的:(KT 高阶函数Lambda)