=================
== The Archive ==
=================

Use Elvis Operator Continuously in Kotlin

|

Introduction

두 번 연속으로 사용한 경우

Kotlin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import kotlin.random.Random

fun main() {
    repeat(10) {
        println("times: $it")
        doubleElvisOperatorTest()
        println()
    }
}

private fun doubleElvisOperatorTest() {
    val firstNullableString = getStringOrNull()
    val secondNullableString = getStringOrNull()
    println("firstNullableString: $firstNullableString, secondNullableString: $secondNullableString")

    val doubleElvisOperator =
        firstNullableString
            ?.let {
                println("firstNullableString length: ${firstNullableString.length}")
                firstNullableString
            }
            ?: secondNullableString
            ?: error("Both NullableString are null")

    println("doubleElvisOperator: $doubleElvisOperator")
}

private fun getStringOrNull() =
    if (Random.nextBoolean()) {
        "String"
    } else {
        null
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
times: 0
firstNullableString: String, secondNullableString: String
firstNullableString length: 6
doubleElvisOperator: String

times: 1
firstNullableString: String, secondNullableString: null
firstNullableString length: 6
doubleElvisOperator: String

times: 2
firstNullableString: null, secondNullableString: null
Exception in thread "main" java.lang.IllegalStateException: Both NullableString are null
	at UseElvisOperatorContinuouslyKt.doubleElvisOperatorTest(UseElvisOperatorContinuously.kt:23)
	at UseElvisOperatorContinuouslyKt.main(UseElvisOperatorContinuously.kt:6)
	at UseElvisOperatorContinuouslyKt.main(UseElvisOperatorContinuously.kt)

Process finished with exit code 1

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import kotlin.Metadata;
import kotlin.random.Random;

@Metadata(
   mv = {1, 9, 0},
   k = 2,
   xi = 48,
   d1 = {"\u0000\u0010\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\u001a\b\u0010\u0000\u001a\u00020\u0001H\u0002\u001a\n\u0010\u0002\u001a\u0004\u0018\u00010\u0003H\u0002\u001a\u0006\u0010\u0004\u001a\u00020\u0001¨\u0006\u0005"},
   d2 = {"doubleElvisOperatorTest", "", "getStringOrNull", "", "main", "kotlin"}
)
public final class UseElvisOperatorContinuouslyKt {
   public static final void main() {
      byte var0 = 10;

      for(int var1 = 0; var1 < var0; ++var1) {
         int it = var1;
         int var3 = false;
         String var4 = "times: " + it;
         System.out.println(var4);
         doubleElvisOperatorTest();
         System.out.println();
      }

   }

   private static final void doubleElvisOperatorTest() {
      String firstNullableString = getStringOrNull();
      String secondNullableString = getStringOrNull();
      String doubleElvisOperator = "firstNullableString: " + firstNullableString + ", secondNullableString: " + secondNullableString;
      System.out.println(doubleElvisOperator);
      String var10000;
      if (firstNullableString != null) {
         int var6 = false;
         String var7 = "firstNullableString length: " + firstNullableString.length();
         System.out.println(var7);
         var10000 = firstNullableString;
      } else {
         if (secondNullableString == null) {
            throw new IllegalStateException("Both NullableString are null".toString());
         }

         var10000 = secondNullableString;
      }

      doubleElvisOperator = var10000;
      String var3 = "doubleElvisOperator: " + doubleElvisOperator;
      System.out.println(var3);
   }

   private static final String getStringOrNull() {
      return Random.Default.nextBoolean() ? "String" : null;
   }

   // $FF: synthetic method
   public static void main(String[] args) {
      main();
   }
}

세 번 연속으로 사용한 경우

Kotlin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import kotlin.random.Random

fun main() {
    repeat(10) {
        println("times: $it")
        tripleElvisOperatorTest()
        println()
    }
}

private fun doubleElvisOperatorTest() {
    val firstNullableString = getStringOrNull()
    val secondNullableString = getStringOrNull()
    println("firstNullableString: $firstNullableString, secondNullableString: $secondNullableString")

    val doubleElvisOperator =
        firstNullableString
            ?.let {
                println("firstNullableString length: ${firstNullableString.length}")
                firstNullableString
            }
            ?: secondNullableString
            ?: error("Both NullableString are null")

    println("doubleElvisOperator: $doubleElvisOperator")
}

private fun tripleElvisOperatorTest() {
    val firstNullableString = getStringOrNull()
    val secondNullableString = getStringOrNull()
    val thirdNullableString = getStringOrNull()
    println(
        "firstNullableString: $firstNullableString, secondNullableString: $secondNullableString, thirdNullableString: $thirdNullableString",
    )

    val tripleElvisOperator =
        firstNullableString
            ?.let {
                println("firstNullableString length: ${firstNullableString.length}")
                firstNullableString
            }
            ?: secondNullableString?.also {
                println("secondNullableString length: ${secondNullableString.length}")
            }
            ?: thirdNullableString
                ?.also {
                    println("thirdNullableString length: ${thirdNullableString.length}")
                }
            ?: error("All NullableStrings are null")

    println("tripleElvisOperator: $tripleElvisOperator")
}

private fun getStringOrNull() =
    if (Random.nextBoolean()) {
        "String"
    } else {
        null
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
times: 0
firstNullableString: String, secondNullableString: String, thirdNullableString: null
firstNullableString length: 6
tripleElvisOperator: String

times: 1
firstNullableString: null, secondNullableString: String, thirdNullableString: String
secondNullableString length: 6
tripleElvisOperator: String

times: 2
firstNullableString: null, secondNullableString: String, thirdNullableString: null
secondNullableString length: 6
tripleElvisOperator: String

times: 3
firstNullableString: null, secondNullableString: null, thirdNullableString: String
thirdNullableString length: 6
tripleElvisOperator: String

times: 4
firstNullableString: null, secondNullableString: String, thirdNullableString: null
secondNullableString length: 6
tripleElvisOperator: String

times: 5
firstNullableString: null, secondNullableString: String, thirdNullableString: null
secondNullableString length: 6
tripleElvisOperator: String

times: 6
firstNullableString: null, secondNullableString: String, thirdNullableString: String
secondNullableString length: 6
tripleElvisOperator: String

times: 7
firstNullableString: String, secondNullableString: null, thirdNullableString: String
firstNullableString length: 6
tripleElvisOperator: String

times: 8
firstNullableString: String, secondNullableString: String, thirdNullableString: null
firstNullableString length: 6
tripleElvisOperator: String

times: 9
firstNullableString: null, secondNullableString: null, thirdNullableString: null
Exception in thread "main" java.lang.IllegalStateException: All NullableStrings are null
	at UseElvisOperatorContinuouslyKt.tripleElvisOperatorTest(UseElvisOperatorContinuously.kt:49)
	at UseElvisOperatorContinuouslyKt.main(UseElvisOperatorContinuously.kt:6)
	at UseElvisOperatorContinuouslyKt.main(UseElvisOperatorContinuously.kt)

Process finished with exit code 1

Java

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
import kotlin.Metadata;
import kotlin.random.Random;

@Metadata(
   mv = {1, 9, 0},
   k = 2,
   xi = 48,
   d1 = {"\u0000\u0010\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\u001a\b\u0010\u0000\u001a\u00020\u0001H\u0002\u001a\n\u0010\u0002\u001a\u0004\u0018\u00010\u0003H\u0002\u001a\u0006\u0010\u0004\u001a\u00020\u0001\u001a\b\u0010\u0005\u001a\u00020\u0001H\u0002¨\u0006\u0006"},
   d2 = {"doubleElvisOperatorTest", "", "getStringOrNull", "", "main", "tripleElvisOperatorTest", "kotlin"}
)
public final class UseElvisOperatorContinuouslyKt {
   public static final void main() {
      byte var0 = 10;

      for(int var1 = 0; var1 < var0; ++var1) {
         int it = var1;
         int var3 = false;
         String var4 = "times: " + it;
         System.out.println(var4);
         tripleElvisOperatorTest();
         System.out.println();
      }

   }

   private static final void doubleElvisOperatorTest() {
      String firstNullableString = getStringOrNull();
      String secondNullableString = getStringOrNull();
      String doubleElvisOperator = "firstNullableString: " + firstNullableString + ", secondNullableString: " + secondNullableString;
      System.out.println(doubleElvisOperator);
      String var10000;
      if (firstNullableString != null) {
         int var6 = false;
         String var7 = "firstNullableString length: " + firstNullableString.length();
         System.out.println(var7);
         var10000 = firstNullableString;
      } else {
         if (secondNullableString == null) {
            throw new IllegalStateException("Both NullableString are null".toString());
         }

         var10000 = secondNullableString;
      }

      doubleElvisOperator = var10000;
      String var3 = "doubleElvisOperator: " + doubleElvisOperator;
      System.out.println(var3);
   }

   private static final void tripleElvisOperatorTest() {
      String firstNullableString = getStringOrNull();
      String secondNullableString = getStringOrNull();
      String thirdNullableString = getStringOrNull();
      String tripleElvisOperator = "firstNullableString: " + firstNullableString + ", secondNullableString: " + secondNullableString + ", thirdNullableString: " + thirdNullableString;
      System.out.println(tripleElvisOperator);
      String var10000;
      boolean var7;
      String var8;
      if (firstNullableString != null) {
         var7 = false;
         var8 = "firstNullableString length: " + firstNullableString.length();
         System.out.println(var8);
         var10000 = firstNullableString;
      } else if (secondNullableString != null) {
         var7 = false;
         var8 = "secondNullableString length: " + secondNullableString.length();
         System.out.println(var8);
         var10000 = secondNullableString;
      } else {
         if (thirdNullableString != null) {
            int var10 = false;
            String var9 = "thirdNullableString length: " + thirdNullableString.length();
            System.out.println(var9);
            var10000 = thirdNullableString;
         } else {
            var10000 = null;
         }

         String var5 = var10000;
         if (var5 == null) {
            throw new IllegalStateException("All NullableStrings are null".toString());
         }

         var10000 = var5;
      }

      tripleElvisOperator = var10000;
      String var4 = "tripleElvisOperator: " + tripleElvisOperator;
      System.out.println(var4);
   }

   private static final String getStringOrNull() {
      return Random.Default.nextBoolean() ? "String" : null;
   }

   // $FF: synthetic method
   public static void main(String[] args) {
      main();
   }
}

Conclusion

References

Categories:

Tags: