为 Kotlin 中的 Boolean 类扩展一套流式 API

近日拜读了大佬 mikyou 一篇讲 Kotlin 中泛型的 文章,里面说到 Kotlin 中泛型应用场景的问题时提到了给 Boolean 扩展出一套流式 API 的点子,甚是精彩,举个栗子:

(3 < 2).yes {
            toast("yes")
        }.otherwise {
            toast("otherwise")
        }

当然上面的代码还体现不出来给 Boolean 扩展流式 API 的优越性,我就是随便举个栗子给大家看看~

源码如下:

sealed class BooleanExt<out T>//起桥梁作用的中间类,定义成协变

object Otherwise : BooleanExt<Nothing>()//Nothing是所有类型的子类型,协变的类继承关系和泛型参数类型继承关系一致

class TransferData<T>(val data: T) : BooleanExt<T>()//data只涉及到了只读的操作

//声明成inline函数
inline fun <T> Boolean.yes(block: () -> T): BooleanExt<T> = when {
    this -> {
        TransferData(block.invoke())
    }
    else -> Otherwise
}

inline fun <T> BooleanExt<T>.otherwise(block: () -> T): T = when (this) {
    is Otherwise ->
        block()
    is TransferData ->
        this.data
}

很骚的操作有木有,不过我看后还是产生了点私人想法,这里调用这套流式 API
的时候,必须先判断肯定,再判断否定,我在想能不能再扩展下可以做到先判断否定,再判断肯定也没问题:

(3 < 2).otherwise {
            toast("otherwise")
        }.yes {
            toast("yes")
        }

当然没问题了!不过这得多定义个中间类,两个中间类,最终定义在两个文件里兼顾两种调用顺序。废话不多说,我们直接贴代码,就是在项目里定义两个文件,一个 BooleanExt1,一个 BooleanExt2:

/**
 * Created by xiaofei on 2018/12/30.
 * desc:Boolean Extension1, Say Goodbye to if-else expression
 */

sealed class BooleanExt1<out T>////起桥梁作用的中间类,定义成协变

object Otherwise : BooleanExt1<Nothing>()//Nothing是所有类型的子类型,协变的类继承关系和泛型参数类型继承关系一致

class TransferData<T>(val data: T) : BooleanExt1<T>()//data只涉及到了只读的操作

//声明成inline函数
inline fun <T> Boolean.yes(block: () -> T): BooleanExt1<T> = when {
    this -> {
        TransferData(block.invoke())
    }
    else -> Otherwise
}

inline fun <T> BooleanExt1<T>.otherwise(block: () -> T): T = when (this) {
    is Otherwise ->
        block()
    is TransferData ->
        this.data
}

and

/**
 * Created by xiaofei on 2018/12/30.
 * desc:Boolean Extension2, Say Goodbye to if-else expression
 */

sealed class BooleanExt2<out T>////起桥梁作用的中间类,定义成协变

object Yes : BooleanExt2<Nothing>()//Nothing是所有类型的子类型,协变的类继承关系和泛型参数类型继承关系一致

class TransferData<T>(val data: T) : BooleanExt2<T>()//data只涉及到了只读的操作

//声明成inline函数
inline fun <T> Boolean.otherwise(block: () -> T): BooleanExt2<T> = when {
    !this -> {
        TransferData(block.invoke())
    }
    else -> Yes
}

inline fun <T> BooleanExt2<T>.yes(block: () -> T): T = when (this) {
    is Yes ->
        block()
    is TransferData ->
        this.data
}

这两个文件定义好之后,使用起来就可以很随意了:

(3 < 2).yes {
            toast("yes")
        }.otherwise {
            toast("otherwise")
        }

(3 <2 ).otherwise {
            toast("otherwise")
        }.yes {
            toast("yes")
        }

完!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章