Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "duplicate in when" in Kotlin?

Tags:

kotlin

I write code like bellow:

when(month){
        1 or 7 -> arrHoangDao = arrayListOf("Tý", "Sửu", "Tỵ", "Mùi")
        2 or 8 -> arrHoangDao = arrayListOf("Dần", "Mão", "Mùi", "Dậu")
        3 or 9 -> arrHoangDao = arrayListOf("Thìn", "Tỵ", "Dậu", "Hợi")
        4 or 10 -> arrHoangDao = arrayListOf("Ngọ", "Mùi", "Sửu", "Dậu")
        5 or 11 -> arrHoangDao = arrayListOf("Thân", "Dậu", "Sửu", "Mão")
        /* 6 or 12 is duplicate */
        6 or 12 -> arrHoangDao = arrayListOf("Tuất", "Hợi", "Mão", "Tị")
    }

I get the message is "duplicate in when". What does it mean?

like image 457
mducc Avatar asked Jul 09 '18 15:07

mducc


1 Answers

You're using bitwise OR with or. This means that 1 or 7 evaluates to 7 and 4 or 10 evaluates to 14. 6 or 12 also happens to evaluate to 14, hence the compiler tells you it is a duplicate.

Solution: use , instead of or. See more on how to use when statements here.

like image 143
Tim Avatar answered Sep 22 '22 20:09

Tim