Kotlin 中的contract 到底有什么用?

我们在开发中肯定会经常用Kotlin提供的一些通用拓展函数,当我们进去看源码的时候会发现许多函数里面有contract {}包裹的代码块,那么这些代码块到底有什么作用呢??

测试

接下来用以下两个我们常用的拓展函数作为例子

public inline fun <T, R> T.run(block: T.() -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block()
}

public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
        returns(false) implies (this@isNullOrEmpty != null)
    }

    return this == null || this.length == 0
}
复制代码

runisNullOrEmpty我相信大家在开发中是经常见到的。

不知道那些代码有什么作用,那么我们就把那几行代码去掉,然后看看函数使用起来有什么区别。

public inline fun <T, R> T.runWithoutContract(block: T.() -> R): R {
    return block()
}

public inline fun CharSequence?.isNullOrEmptyWithoutContract(): Boolean {
    return this == null || this.length == 0
}
复制代码

上面是去掉了contract{}代码块后的两个函数 调用看看

fun test() {
    var str1: String = ""
    var str2: String = ""

    runWithoutContract {
        str1 = "jayce"
    }
    run {
        str2 = "jayce"
    }

    println(str1) //jayce
    println(str2) //jayce
}
复制代码

经过测试发现,看起来好像没什么问题,run代码块都能都正常执行,做了赋值的操作。

那么如果是这样呢

将str的初始值去掉,在run代码块里面进行初始化操作

@Test
fun test() {
    var str1: String
    var str2: String 

    runWithoutContract {
        str1 = "jayce"
    }
    run {
        str2 = "jayce"
    }

    println(str1) //编译不通过 (Variable 'str1' must be initialized)
    println(str2) //编译通过
}
复制代码

??????

我们不是在runWithoutContract做了初始化赋值的操作了吗?怎么IDE还报错,难道是IDE出了什么问题?好 有问题就重启,我去,重启还没解决。。。。好重装。不不不!!别急 会不会Contract代码块就是干这个用的?是不是它悄悄的跟IDE说了什么话 以至于它能正常编译通过?

好 这个问题先放一放 我们再看看没contract版本的isNullOrEmpty对比有contract的有什么区别

fun test() {
    val str: String? = "jayce"

    if (!str.isNullOrEmpty()) {
        println(str) //jayce
    }
    if (!str.isNullOrEmptyWithoutContract()) {
        println(str) //jayce
    }
}
复制代码

发现好像还是没什么问题。相信大家根据上面遇到的问题可以猜测,这其中肯定也有坑。

比如这种情况

fun test() {
    val str: String? = "jayce"

    if (!str.isNullOrEmpty()) {
        println(str.length) // 编译通过
    }

    if (!str.isNullOrEmptyWithoutContract()) {
        println(str.length) // 编译不通过(Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?)
    }
}
复制代码

根据错误提示可以看出,在isNullOrEmptyWithoutContract判断为flase之后的代码块,str这个字段还是被IDE认为是一个可空类型,必须要进行空检查才能通过。然而在isNullOrEmpty返回flase之后的代码块,IDE认为str其实已经是非空了,所以使用前就不需要进行空检查。

查看 contract 函数

public inline fun contract(builder: ContractBuilder.() -> Unit) { }
复制代码

点进去源码,我们可以看到contract是一个内联函数,接收一个函数类型的参数,该函数是ContractBuilder的一个拓展函数(也就是说在这个函数体里面拥有ContractBuilder的上下文)

看看ContractBuilder给我们提供了哪些函数(主要就是依靠这些函数来约定我们自己写的lambda函数)

public interface ContractBuilder {
  	//描述函数正常返回,没有抛出任何异常的情况。
    @ContractsDsl public fun returns(): Returns

  	//描述函数以value返回的情况,value可以取值为 true|false|null。
    @ContractsDsl public fun returns(value: Any?): Returns
  
  	//描述函数以非null值返回的情况。
    @ContractsDsl public fun returnsNotNull(): ReturnsNotNull

   	//描述lambda会在该函数调用的次数,次数用kind指定
    @ContractsDsl public fun <R> callsInPlace(lambda: Function<R>, kind: InvocationKind = InvocationKind.UNKNOWN): CallsInPlace
}
复制代码
returns

其中 returns() returns(value) returnsNotNull() 都会返回一个继承于SimpleEffectReturns 接下来看看SimpleEffect

public interface SimpleEffect : Effect {
  	//接收一个Boolean值的表达式 改函数用来表示当SimpleEffect成立之后 保证Boolean值的表达式返回值为true
  	//表达式可以传判空代码块(`== null`, `!= null`)判断实例语句 (`is`, `!is`)。
    public infix fun implies(booleanExpression: Boolean): ConditionalEffect
}
复制代码

可以看到SimpleEffect里面有一个中缀函数implies 。可以使用ContractBuilder的函数指定某种返回的情况 然后用implies来声明传入的表达式为true。

看到这里 那么我们应该就知道 isNullOrEmpty() 加的contract是什么意思了

public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
      	//返回值为false的情况 returns(false)
				//意味着 implies
				//调用该函数的对象不为空 (this@isNullOrEmpty != null)
        returns(false) implies (this@isNullOrEmpty != null)
    }
  
    return this == null || this.length == 0
}
复制代码

因为isNullOrEmpty里面加了contract代码块,告诉IDE说:返回值为false的情况意味着调用该函数的对象不为空。所以我们就可以直接在判断语句后直接使用非空的对象了。

有些同学可能还是不理解,这里再举一个没什么用的例子(运行肯定会crash哈。。。)

@ExperimentalContracts //因为该特性还在试验当中 所以需要加上这个注解
fun CharSequence?.isNotNull(): Boolean {
    contract {
      	//返回值为true returns(true)
      	//意味着implies
      	//调用该函数的对象是StringBuilder (this@isNotNull is StringBuilder)
        returns(true) implies (this@isNotNull is StringBuilder)
    }

    return this != null
}

fun test() {                                                                                               
    val str: String? = "jayce"                                                                             
                                                                                                           
    if (str.isNotNull()) {                                                                                 
        str.append("")//String可是没有这个函数的,因为我们用contract让他强制转换成StringBuilder了 所以才有了这个函数                       
    }                                                                                                      
}
复制代码

是的 这样IDE居然没有报错,因为经过我们contract的声明,只要这个函数返回true,调用函数的对象就是一个StringBuilder。

callsInPlace
//描述lambda会在该函数调用的次数,次数用kind指定
@ContractsDsl public fun <R> callsInPlace(lambda: Function<R>, kind: InvocationKind = InvocationKind.UNKNOWN): CallsInPlace
复制代码

可以知道callsInPlace是用来指定lambda函数调用次数的

kind有四种取值

  • InvocationKind.AT_MOST_ONCE:最多调用一次
  • InvocationKind.AT_LEAST_ONCE:最少调用一次
  • InvocationKind.EXACTLY_ONCE:调用一次
  • InvocationKind.UNKNOWN:未知,不指定的默认值

我们再看回去之前run函数里面的contract声明了什么

public inline fun <T, R> T.run(block: T.() -> R): R {
    contract {
      	//block这个函数,刚好调用一次
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block()
}
复制代码

看到这里 应该就知道为什么我们自己写的runWithoutContract会报错(Variable 'str1' must be initialized),而系统的run却不会报错了,因为run声明了lambda会调用一次,所以就一定会对str2做初始化操作,然而runWithoutContract却没有声明,所以IDE就会报错(因为有可能不会调用,所以就不会做初始化操作了)。

总结

  1. Kotlin提供了一些自动转换的功能,例如平时判空和判断是否为某个实例的时候,Kotlin都会为我们自动转换。但是如果这个判断被提取到其他函数的时候,这个转换会失效。所以提供了contract给我们在函数体添加声明,编译器会遵守我们的约定。
  2. 当使用一个高阶函数的时候,可以使用callsInPlace指定该函数会被调用的次。例如在函数体里面做初始化,如果申明为EXACTLY_ONCE的时候,IDE就不会报错,因为编译器会遵守我们的约定。

Guess you like

Origin juejin.im/post/7051907516380217374