dave08
11/22/2018, 12:49 PMval foo: String = prefs.getString("foo", "") ?: ""
?Tolriq
11/22/2018, 2:18 PMdave08
11/22/2018, 2:19 PMdave08
11/22/2018, 2:20 PMTolriq
11/22/2018, 2:21 PMdave08
11/22/2018, 2:22 PMdave08
11/22/2018, 2:23 PMTolriq
11/22/2018, 2:23 PMTolriq
11/22/2018, 2:25 PMgildor
11/23/2018, 3:51 AMgildor
11/23/2018, 3:51 AMTolriq
11/23/2018, 8:46 AMgildor
11/23/2018, 9:33 AMgildor
11/23/2018, 9:33 AMprefs.getString("foo", "")!!
also worksgildor
11/23/2018, 9:34 AMTolriq
11/23/2018, 9:54 AMgildor
11/23/2018, 9:56 AMusing ?: will have generated code that do an if result is null then do thatIt’s not true.code after ?: will be used, because we passed null as default value
gildor
11/23/2018, 9:57 AMTolriq
11/23/2018, 9:57 AMTolriq
11/23/2018, 9:57 AMgildor
11/23/2018, 10:01 AMgildor
11/23/2018, 10:03 AM!!
also generates bytecode for null check. And if you really care about it, better to reimplement getString()
as extension using get()
, instead of getString() + null assertTolriq
11/23/2018, 10:13 AMgildor
11/23/2018, 10:14 AMgetString("Foo", "") ?: ""
or something like thatTolriq
11/23/2018, 10:15 AMgildor
11/23/2018, 10:16 AMthat’s not that coolThis is assert, obviously you need some code that will check for null and thow an exception. But I really don’t understand why do you care about it, everything will be optimized with jit, and even without jit null check is nothing comparing with map lookup and syncronization, so it’s just nothing
Tolriq
11/23/2018, 10:19 AM-assumenosideeffects class kotlin.jvm.internal.Intrinsics {
static void checkParameterIsNotNull(java.lang.Object, java.lang.String);
}
In proguard as the dex size impact is non negligeable and the checks only brings better stack traces at the expense of a small if check. But on a large app a million null check is still something. And if you have proper tested code in the end there's no crash and so no stack trace to generate.gildor
11/23/2018, 10:20 AMgildor
11/23/2018, 10:22 AMTolriq
11/23/2018, 10:22 AMTolriq
11/23/2018, 10:23 AMgildor
11/23/2018, 10:26 AMTolriq
11/23/2018, 10:27 AMTolriq
11/23/2018, 10:27 AMgildor
11/23/2018, 10:28 AMgildor
11/23/2018, 10:29 AMYou doubt I’ve tested and R8 guys confirmedThis is micro benchmarking, which is fine for compiler developers, but you real application probably do something more than check for nulls
gildor
11/23/2018, 10:29 AMTolriq
11/23/2018, 10:33 AM