Use ternary 'if' for 'when' (and also '||' as ',' in 'when').

This commit is contained in:
Dmitry Petrov
2016-08-22 09:59:42 +03:00
committed by Dmitry Petrov
parent c36a0d04ce
commit c0d521266b
22 changed files with 343 additions and 306 deletions
+2 -2
View File
@@ -16,12 +16,12 @@ IrFile /elvis.kt
IrFunction public fun test3(/*0*/ a: kotlin.Any?, /*1*/ b: kotlin.Any?): kotlin.String
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
WHEN subject=null type=kotlin.Unit
IF type=kotlin.Unit operator=IF
if: TYPE_OP operator=NOT_INSTANCEOF typeOperand=kotlin.String
GET_VAR b type=kotlin.Any? operator=null
then: RETURN type=<no-type>
LITERAL String type=kotlin.String value=''
WHEN subject=null type=kotlin.Unit
IF type=kotlin.Unit operator=IF
if: TYPE_OP operator=NOT_INSTANCEOF typeOperand=kotlin.String?
GET_VAR a type=kotlin.Any? operator=null
then: RETURN type=<no-type>
+7 -6
View File
@@ -3,13 +3,14 @@ IrFile /ifElseIf.kt
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
RETURN type=<no-type>
WHEN subject=null type=kotlin.Int
IF type=kotlin.Int operator=IF
if: BINARY_OP operator=GT type=kotlin.Boolean related=public open override /*1*/ fun compareTo(/*0*/ other: kotlin.Int): kotlin.Int
GET_VAR i type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='0'
then: LITERAL Int type=kotlin.Int value='1'
if: BINARY_OP operator=LT type=kotlin.Boolean related=public open override /*1*/ fun compareTo(/*0*/ other: kotlin.Int): kotlin.Int
GET_VAR i type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='0'
then: DUMMY MINUS type=kotlin.Int
else: LITERAL Int type=kotlin.Int value='0'
else: IF type=kotlin.Int operator=IF
if: BINARY_OP operator=LT type=kotlin.Boolean related=public open override /*1*/ fun compareTo(/*0*/ other: kotlin.Int): kotlin.Int
GET_VAR i type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='0'
then: DUMMY MINUS type=kotlin.Int
else: LITERAL Int type=kotlin.Int value='0'
+3 -3
View File
@@ -18,7 +18,7 @@ IrFile /smartCasts.kt
IrFunction public fun test1(/*0*/ x: kotlin.Any): kotlin.Unit
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
WHEN subject=null type=kotlin.Unit
IF type=kotlin.Unit operator=IF
if: TYPE_OP operator=NOT_INSTANCEOF typeOperand=kotlin.String
GET_VAR x type=kotlin.Any operator=null
then: RETURN type=<no-type>
@@ -40,7 +40,7 @@ IrFile /smartCasts.kt
IrFunction public fun test2(/*0*/ x: kotlin.Any): kotlin.String
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
WHEN subject=null type=kotlin.Unit
IF type=kotlin.Unit operator=IF
if: TYPE_OP operator=NOT_INSTANCEOF typeOperand=kotlin.String
GET_VAR x type=kotlin.Any operator=null
then: RETURN type=<no-type>
@@ -52,7 +52,7 @@ IrFile /smartCasts.kt
IrFunction public fun test3(/*0*/ x: kotlin.Any): kotlin.String
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
WHEN subject=null type=kotlin.Unit
IF type=kotlin.Unit operator=IF
if: TYPE_OP operator=NOT_INSTANCEOF typeOperand=kotlin.String
GET_VAR x type=kotlin.Any operator=null
then: RETURN type=<no-type>
@@ -14,7 +14,7 @@ IrFile /smartCastsWithDestructuring.kt
IrFunction public fun test(/*0*/ x: I1): kotlin.Unit
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
WHEN subject=null type=kotlin.Unit
IF type=kotlin.Unit operator=IF
if: TYPE_OP operator=NOT_INSTANCEOF typeOperand=I2
GET_VAR x type=I1 operator=null
then: RETURN type=<no-type>
+8
View File
@@ -17,3 +17,11 @@ fun test(x: Any?) =
x in setOf<Nothing>() -> "nothingness?"
else -> "something"
}
fun testComma(x: Int) =
when (x) {
1, 2, 3, 4 -> "1234"
5, 6, 7 -> "567"
8, 9 -> "89"
else -> "?"
}
+91 -31
View File
@@ -4,45 +4,105 @@ IrFile /when.kt
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
RETURN type=<no-type>
WHEN subject=tmp0_subject type=kotlin.String
$subject: VAR val tmp0_subject: kotlin.Any?
BLOCK type=kotlin.String hasResult=true operator=WHEN
VAR val tmp0_subject: kotlin.Any?
GET_VAR x type=kotlin.Any? operator=null
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Any? operator=null
LITERAL Null type=kotlin.Nothing? value='null'
then: LITERAL String type=kotlin.String value='null'
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Any? operator=null
GET_OBJECT A type=A
then: LITERAL String type=kotlin.String value='A'
if: TYPE_OP operator=INSTANCEOF typeOperand=kotlin.String
GET_VAR tmp0_subject type=kotlin.Any? operator=null
then: LITERAL String type=kotlin.String value='String'
if: CALL .contains type=kotlin.Boolean operator=IN
$receiver: CALL .setOf type=kotlin.collections.Set<kotlin.Nothing> operator=null
element: TYPE_OP operator=IMPLICIT_CAST typeOperand=kotlin.Any
IF type=kotlin.String operator=WHEN
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Any? operator=null
then: LITERAL String type=kotlin.String value='nothingness?'
else: LITERAL String type=kotlin.String value='something'
LITERAL Null type=kotlin.Nothing? value='null'
then: LITERAL String type=kotlin.String value='null'
else: IF type=kotlin.String operator=WHEN
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Any? operator=null
GET_OBJECT A type=A
then: LITERAL String type=kotlin.String value='A'
else: IF type=kotlin.String operator=WHEN
if: TYPE_OP operator=INSTANCEOF typeOperand=kotlin.String
GET_VAR tmp0_subject type=kotlin.Any? operator=null
then: LITERAL String type=kotlin.String value='String'
else: IF type=kotlin.String operator=WHEN
if: CALL .contains type=kotlin.Boolean operator=IN
$receiver: CALL .setOf type=kotlin.collections.Set<kotlin.Nothing> operator=null
element: TYPE_OP operator=IMPLICIT_CAST typeOperand=kotlin.Any
GET_VAR tmp0_subject type=kotlin.Any? operator=null
then: LITERAL String type=kotlin.String value='nothingness?'
else: LITERAL String type=kotlin.String value='something'
IrFunction public fun test(/*0*/ x: kotlin.Any?): kotlin.String
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
RETURN type=<no-type>
WHEN subject=null type=kotlin.String
IF type=kotlin.String operator=WHEN
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=public open operator fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
GET_VAR x type=kotlin.Any? operator=null
LITERAL Null type=kotlin.Nothing? value='null'
then: LITERAL String type=kotlin.String value='null'
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=public open operator fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
GET_VAR x type=kotlin.Any? operator=null
GET_OBJECT A type=A
then: LITERAL String type=kotlin.String value='A'
if: TYPE_OP operator=INSTANCEOF typeOperand=kotlin.String
GET_VAR x type=kotlin.Any? operator=null
then: LITERAL String type=kotlin.String value='String'
if: CALL .contains type=kotlin.Boolean operator=IN
$receiver: CALL .setOf type=kotlin.collections.Set<kotlin.Nothing> operator=null
element: TYPE_OP operator=IMPLICIT_CAST typeOperand=kotlin.Any
else: IF type=kotlin.String operator=WHEN
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=public open operator fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
GET_VAR x type=kotlin.Any? operator=null
then: LITERAL String type=kotlin.String value='nothingness?'
else: LITERAL String type=kotlin.String value='something'
GET_OBJECT A type=A
then: LITERAL String type=kotlin.String value='A'
else: IF type=kotlin.String operator=WHEN
if: TYPE_OP operator=INSTANCEOF typeOperand=kotlin.String
GET_VAR x type=kotlin.Any? operator=null
then: LITERAL String type=kotlin.String value='String'
else: IF type=kotlin.String operator=WHEN
if: CALL .contains type=kotlin.Boolean operator=IN
$receiver: CALL .setOf type=kotlin.collections.Set<kotlin.Nothing> operator=null
element: TYPE_OP operator=IMPLICIT_CAST typeOperand=kotlin.Any
GET_VAR x type=kotlin.Any? operator=null
then: LITERAL String type=kotlin.String value='nothingness?'
else: LITERAL String type=kotlin.String value='something'
IrFunction public fun testComma(/*0*/ x: kotlin.Int): kotlin.String
IrExpressionBody
BLOCK type=<no-type> hasResult=false operator=null
RETURN type=<no-type>
BLOCK type=kotlin.String hasResult=true operator=WHEN
VAR val tmp0_subject: kotlin.Int
GET_VAR x type=kotlin.Int operator=null
IF type=kotlin.String operator=WHEN
if: IF type=kotlin.Boolean operator=WHEN_COMMA
if: IF type=kotlin.Boolean operator=WHEN_COMMA
if: IF type=kotlin.Boolean operator=WHEN_COMMA
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='1'
then: LITERAL Boolean type=kotlin.Boolean value='true'
else: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='2'
then: LITERAL Boolean type=kotlin.Boolean value='true'
else: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='3'
then: LITERAL Boolean type=kotlin.Boolean value='true'
else: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='4'
then: LITERAL String type=kotlin.String value='1234'
else: IF type=kotlin.String operator=WHEN
if: IF type=kotlin.Boolean operator=WHEN_COMMA
if: IF type=kotlin.Boolean operator=WHEN_COMMA
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='5'
then: LITERAL Boolean type=kotlin.Boolean value='true'
else: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='6'
then: LITERAL Boolean type=kotlin.Boolean value='true'
else: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='7'
then: LITERAL String type=kotlin.String value='567'
else: IF type=kotlin.String operator=WHEN
if: IF type=kotlin.Boolean operator=WHEN_COMMA
if: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='8'
then: LITERAL Boolean type=kotlin.Boolean value='true'
else: BINARY_OP operator=EQEQ type=kotlin.Boolean related=null
GET_VAR tmp0_subject type=kotlin.Int operator=null
LITERAL Int type=kotlin.Int value='9'
then: LITERAL String type=kotlin.String value='89'
else: LITERAL String type=kotlin.String value='?'