Fuse primitive equality with safe call to avoid boxing
In code like 'a?.b == 42', we can immediately generate equality comparison result when receiver is null (false for '==', true for '!='), since the primitive value is definitely non-null. Otherwise unnecessary boxing/unboxing is generated to handle possibly null result of 'a?.b'.
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
fun Long.id() = this
|
||||
|
||||
fun String.drop2() = if (length >= 2) subSequence(2, length) else null
|
||||
|
||||
fun doSimple1(s: String?) = s?.length == 3
|
||||
|
||||
fun doLongReceiver1(x: Long) = x?.id() == 3L
|
||||
|
||||
fun doChain1(s: String?) = s?.drop2()?.length == 1
|
||||
|
||||
fun doIf1(s: String?) =
|
||||
if (s?.length == 1) "A" else "B"
|
||||
|
||||
fun doSimple2(s: String?) = 3 == s?.length
|
||||
|
||||
fun doLongReceiver2(x: Long) = 3L == x?.id()
|
||||
|
||||
fun doChain2(s: String?) = 1 == s?.drop2()?.length
|
||||
|
||||
fun doIf2(s: String?) =
|
||||
if (1 == s?.length) "A" else "B"
|
||||
|
||||
// 0 valueOf
|
||||
Reference in New Issue
Block a user