nadi
03/03/2024, 4:21 PMnadi
03/03/2024, 4:22 PMephemient
03/03/2024, 4:24 PMval val1 = func1()
val val2 = func2()
check(val1 == null || val2 == null)
return val1 ?: val2
nadi
03/03/2024, 4:24 PMJoffrey
03/03/2024, 5:27 PMKlitos Kyriacou
03/04/2024, 10:13 AM!!
) but checking for null is ok.Joffrey
03/04/2024, 10:14 AM?: error("describe the broken invariant here")
over just !!
. Because it also auto-documents why it's not expected to be null, and it does so very clearly if it ever failsephemient
03/04/2024, 4:02 PMcheckNotNull()
, requireNotNull()
Joffrey
03/04/2024, 5:00 PM!!
without explanationKlitos Kyriacou
03/04/2024, 5:25 PMmap["existing-key"]!!
you can do map.getValue("existing-key")
. It's a shame that getValue
is so much more to type than []
.ephemient
03/04/2024, 5:29 PMval existingKey by map // map["existingKey"]
although it's kind of problematic for keys with names like existing-key
Joffrey
03/04/2024, 5:32 PMgetValue
is slightly better than !!
regarding the error message, because it describes more clearly what happened (in case of error). However, it still doesn't explain why you thought this would never be null, no tooling can help here. But yeah, sometimes it's good enough (like when the previous line of code is literally checking your invariant)