目录
  • 前言
  • 测试
  • 总结

前言

我们在开发中肯定会经常用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提供了一些自动转换的功能,例如平时判空和判断是否为某个实例的时候,kotlin都会为我们自动转换。但是如果这个判断被提取到其他函数的时候,这个转换会失效。所以提供了contract给我们在函数体添加声明,编译器会遵守我们的约定。

当使用一个高阶函数的时候,可以使用callsinplace指定该函数会被调用的次。例如在函数体里面做初始化,如果申明为exactly_once的时候,ide就不会报错,因为编译器会遵守我们的约定。

到此这篇关于kotlin中的contract到底有什么用的文章就介绍到这了,更多相关kotlin contract用处内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!