温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Kotlin中的contract怎么用

发布时间:2022-01-20 11:01:52 来源:亿速云 阅读:118 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关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
}

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

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

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() 都会返回一个继承于SimpleEffect的Returns 接下来看看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就会报错(因为有可能不会调用,所以就不会做初始化操作了)。

关于“Kotlin中的contract怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI