kotlin-null-safety

Is `a?.let{} ?: run{}` idiomatic in Kotlin?

梦想的初衷 提交于 2019-12-03 14:06:47
I saw the following comment in a S.O. post, and I'm intrigued: why don't you use if for null checks? a?.let{} ?: run{} is only appropriate in rare cases, otherwise it is not idiomatic – voddan May 15 '16 at 7:29 best way to null check in kotlin? Why is that construct "only appropriate in rare cases"? The lead engineer for Kotlin says, run allows you to use multiple statements on the right side of an elvis operator https://stackoverflow.com/a/51241983/6656019 although I admit that's not actually endorsing it as idiomatic. Both of these posts seem to be from very well respected S.O. Kotlin

Idiomatic way of handling nullable or empty List in Kotlin

大憨熊 提交于 2019-12-03 04:50:32
问题 Say I have a variable activities of type List<Any>? . If the list is not null and not empty, I want to do something, otherwise I want to do something else. I came up with following solution: when { activities != null && !activities.empty -> doSomething else -> doSomethingElse } Is there a more idiomatic way to do this in Kotlin? 回答1: For some simple actions you can use the safe call operator, assuming the action also respects not operating on an empty list (to handle your case of both null

Why doesn't toString throw an exception when called on null value in Kotlin? [duplicate]

独自空忆成欢 提交于 2019-12-02 07:37:12
问题 This question already has answers here : Why do unsafe .run() call works fine on a null value in Kotlin? (3 answers) Closed last year . Given the code fun main(args: Array<String>) { val someText: String? = null println(someText.toString()) } When run, output is null Two questions appear: is that possible to implement custom null-safe method with fallback to some default code (like, I think, toString does) why no exception is thrown? 回答1: From the docs: fun Any?.toString(): String Returns a

Why doesn't toString throw an exception when called on null value in Kotlin? [duplicate]

北慕城南 提交于 2019-12-02 03:46:57
This question already has an answer here: Why do unsafe .run() call works fine on a null value in Kotlin? 3 answers Given the code fun main(args: Array<String>) { val someText: String? = null println(someText.toString()) } When run, output is null Two questions appear: is that possible to implement custom null-safe method with fallback to some default code (like, I think, toString does) why no exception is thrown? From the docs : fun Any?.toString(): String Returns a string representation of the object. Can be called with a null receiver, in which case it returns the string "null". You can

Why do unsafe .run() call works fine on a null value in Kotlin?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 18:14:38
I have the following code fragment: val foo: String? = null foo.run { println("foo") } I have here a nullable variable foo that is actually set to null followed by a nonsafe .run() call. When I run the code snippet, I get foo printed out despite the fact that the run method is called on a null . Why is that? Why no NullPointerException ? Why does compiler allow a nonsafe call on an optional value? If I pass println(foo) , I get a nice juicy null in the console so I think it's safe to assume that foo is actually null . I believe, there are two things that both might be of some surprise: the

Why do unsafe .run() call works fine on a null value in Kotlin?

时光怂恿深爱的人放手 提交于 2019-12-01 16:10:46
问题 I have the following code fragment: val foo: String? = null foo.run { println("foo") } I have here a nullable variable foo that is actually set to null followed by a nonsafe .run() call. When I run the code snippet, I get foo printed out despite the fact that the run method is called on a null . Why is that? Why no NullPointerException ? Why does compiler allow a nonsafe call on an optional value? If I pass println(foo) , I get a nice juicy null in the console so I think it's safe to assume

Kotlin call function only if all arguments are not null

本秂侑毒 提交于 2019-12-01 15:55:43
Is there a way in kotlin to prevent function call if all (or some) arguments are null? For example Having function: fun test(a: Int, b: Int) { /* function body here */ } I would like to prevent null checks in case when arguments are null . For example, for arguments: val a: Int? = null val b: Int? = null I would like to replace: a?.let { b?.let { test(a, b) } } with: test(a, b) I imagine that function definition syntax could look something like this: fun test(@PreventNullCall a: Int, @PreventNullCall b: Int) And that would be equivalent to: fun test(a: Int?, b: Int?) { if(a == null) { return }

kotlin reflection check nullable types

假如想象 提交于 2019-12-01 05:53:37
How can I test if a KType variable holds a value of a nullable kotlin type, (e.G. Int?)? I have var type: KType variable coming from a KProperty<*>.returnType and I need to detect if it is equal to certain kotlin types (Int, Long, etc). This works with: when (type) { Int::class.defaultType -> ... Long::class.defaultType -> ... else -> ... } but this only works for non-nullable types, so the first branch does not match to Int? However I was yet unable to figure out how I could detect is type is Int? other then to obvious but not so nice type.toString().equals("kotlin.Int?") As you can see from

kotlin reflection check nullable types

三世轮回 提交于 2019-12-01 04:45:41
问题 How can I test if a KType variable holds a value of a nullable kotlin type, (e.G. Int?)? I have var type: KType variable coming from a KProperty<*>.returnType and I need to detect if it is equal to certain kotlin types (Int, Long, etc). This works with: when (type) { Int::class.defaultType -> ... Long::class.defaultType -> ... else -> ... } but this only works for non-nullable types, so the first branch does not match to Int? However I was yet unable to figure out how I could detect is type

Best way to null check in Kotlin?

↘锁芯ラ 提交于 2019-11-28 19:04:34
Should I use double = , or triple = ? if(a === null) { //do something } or if(a == null) { //do something } Similarly for 'not equals': if(a !== null) { //do something } or if(a != null) { //do something } Both approaches generate the same bytecode so you can choose whatever you prefer. A structural equality a == b is translated to a?.equals(b) ?: (b === null) Therefore when comparing to null , the structural equality a == null is translated to a referential equality a === null . According to the docs , there is no point in optimizing your code, so you can use a == null and a != null Note that