玩转Kotlin类内部扩展函数

在这里插入图片描述
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)
            }
        }
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fundroid

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值