Kotlin的扩展函数,既可以定义在Top-level,也可以定义在Class内。类内部扩展函数虽然使用起来多了限制,但是避免了对top-level的污染,可以看做为扩展函数增加了作用域或者namespace,使用得当的话也是非常有用的。
基本使用
class SquareScope {
fun Int.square(): Int = this * this
}
上面扩展函数定义在SquareScope内部,无法直接在top-level直接使用。
@Test
fun test() {
println(22.square()) // compile error
}
只有当this
是SquareScope的情况下,才可以使用Int.square
,所以我们可以在SquareScope内部使用
class SquareScope {
fun Int.square(): Int = this * this
@Test
fun test() {
println(22.square())
}
}
也可以借助with
操作符
@Test
fun test() {
// with似的this指向SquareScope
with(SquareScope()) {
println(22.square())
}
}
DSL中使用
上面with(SquareScope())
相当于创建了一个作用域SquareScope,我们也可以用DSL的方式定义这个作用域
class SquareScope {
fun Int.square(): Int = this * this
}
//参数接受一个带receiver的lambda
fun squareScope(block: SquareScope.()-> Unit) = SquareScope().block()
@Test
fun test() {
// DSL中使用扩展方法
squareScope {
println(22.square())
}
}
带参数的DSL
有时候可以将扩展函数的参数添加到DSL上,从而控制函数执行逻辑
class DateFormatScope(private val format: String) {
fun Date.simpleFormat(): String {
return SimpleDateFormat(format, Locale.getDefault()).format(this)
}
}
fun withFormat(format: String, block: DateFormatScope.() -> Unit) =
DateFormatScope(format).block()
如上,format
可以作为simpleFormat
的参数定义,也可以作为DateFormatScope的状态存在,当作为作用域状态时可以作为DSL的参数进行定义
@Test
fun test() {
withFormat("yyyy.MM.dd") {
println(Date().simpleFormat()) // ex. 2020.11.04
}
withFormat("yyyy.MM.dd-HH:mm") {
println(Date().simpleFormat()) // ex. 2020.11.04-11:36
}
}
带泛型的DSL
除了在DSL中配置参数以外,还可以配置泛型
val Activity.contentView: View get() = findViewById<ViewGroup>(android.R.id.content)[0]
fun <T : ViewDataBinding> ComponentActivity.requireBinding(): T =
DataBindingUtil.bind(contentView) ?: error("please setup data binding")
/*
* DataBinding初始化的作用域
*/
fun <T : ViewDataBinding> ComponentActivity.bindingScope(block: BindingScope<T>.()->Unit): T =
requireBinding<T>().apply {
BindingScope(this, this@bindingScope).block()
}
/**
* 此Scope提供用于DataBinding初始化的扩展函数
*/
class BindingScope<T : ViewDataBinding>(val binding: T, private val activity: ComponentActivity) {
init {
binding.lifecycleOwner = activity
}
fun Int.spanGridLayoutManager(): GridLayoutManager = GridLayoutManager(activity, this)
fun RecyclerView.decorationSpace(value: Int) {
addItemDecoration(SpacesItemDecoration(value))
}
}
上面例子通过DSL创建作用域用来进行DataBinding配置,作用域中提供了用于初始化的扩展函数。泛型T代表了DataBinding的类型,整个初始化过程相当简洁
class MainActivity : AppCompatActivity(R.layout.activity_main) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mainBinding = bindingScope<ActivityMainBinding> {
binding.list.apply {
layoutManager = 3.spanGridLayoutManager()
decorationSpace(4)
}
}
}
}