bodiam
09/25/2020, 6:01 AMval myObject: MyClass? = map.ifKeyPresent("xyz) { return MyClass(it }.orElse { null }
So, basically, if a map contains the key, convert the value of that key to a different type, and if it’s not there, return something else, for example null
. Does something like this exist?vineethraj49
09/25/2020, 6:03 AMval myObject = map["xyz"]?.let { MyClass(it) }
bodiam
09/25/2020, 6:03 AMvineethraj49
09/25/2020, 6:03 AMvineethraj49
09/25/2020, 6:03 AMvineethraj49
09/25/2020, 6:03 AMbodiam
09/25/2020, 6:05 AMTobias Berger
09/25/2020, 7:27 AMval myObject = if ("xyz" in map) MyClass(map["xyz"]) else null
but I guess that's a really rare caseMichael de Kaste
09/25/2020, 9:46 AMexistence with null
and non-existence
encapsulate the same meaning, one should ask themselves if making a map with nullable values is what you want to begin with.