Compiler is unable to know that the options are ex...
# announcements
a
Compiler is unable to know that the options are exhausted. I have enum class:
Copy code
enum class SAConf {
	GND,
	VS,
	SDA,
	SCL
}
and when expression
Copy code
fun address(A0: SAConf, A1: SAConf): Int = when { // <- expression requires else branch
	A0 == SAConf.GND && A1 == SAConf.GND -> 0x40
	A0 == SAConf.GND && A1 == SAConf.VS -> 0x41
	A0 == SAConf.GND && A1 == SAConf.SDA -> 0x42
	A0 == SAConf.GND && A1 == SAConf.SCL -> 0x43
	A0 == SAConf.VS && A1 == SAConf.GND -> 0x44
	A0 == SAConf.VS && A1 == SAConf.VS -> 0x45
	A0 == SAConf.VS && A1 == SAConf.SDA -> 0x46
	A0 == SAConf.VS && A1 == SAConf.SCL -> 0x47
	A0 == SAConf.SDA && A1 == SAConf.GND -> 0x48
	A0 == SAConf.SDA && A1 == SAConf.VS -> 0x49
	A0 == SAConf.SDA && A1 == SAConf.SDA -> 0x4A
	A0 == SAConf.SDA && A1 == SAConf.SCL -> 0x4B
	A0 == SAConf.SCL && A1 == SAConf.GND -> 0x4C
	A0 == SAConf.SCL && A1 == SAConf.VS -> 0x4D
	A0 == SAConf.SCL && A1 == SAConf.SDA -> 0x4E
	A0 == SAConf.SCL && A1 == SAConf.SCL -> 0x4F
}
How do i make compiler know that when is already been exhaustive
w
unfortunately, nested
when
is needed.
Copy code
when (A0) {
   SAConf.GND -> when (A1) {
      SAConf.GND -> 0x40
      SAConf.VS  -> 0x41
      ...
   }
   SAConf.VS -> when (A1) {
      ...
   }
   ...
}
a
ohh thanks!
e
maybe Set<EnumMap<SAConf, Int>>?
w
Ah, so you mean that we can do like the follow, right?
Copy code
val addressMap = mapOf(
   SAConf.GND to EnumMap(mapOf(
      SAConf.GND to 0x40,
      SAConf.VS  to 0x41,
      ...
   )),
   SAConf.VS to EnumMap(mapOf(
      SAConf.GND to 0x44,
      SAConf.VS  to 0x45,
      ...
   )),
   ...
)
Copy code
addressMap[A0][A1]
👍 2
e
yep
maybe both codes are O(1), but I'd like it
w
Yes, it’s so cool
e
thx :3
k
This could also work. It's less freedom but should do the same.
💯 1
👍 2
e
:o
I didn't think there's a rule
a
This one is beautiful, but i want to know if 4 instead of 0x04 would work more efficiently or 0x04 is better?
k
Exactly the same