kotlin-null-safety

Confused about Null Safety in Kotlin

左心房为你撑大大i 提交于 2020-11-27 01:58:15
问题 I am new to Kotlin and have read more than one time about Null Safety in Kotlin but I am really confused and do not understand clearly. Can anyone helps me answer questions below: What does ! character in fun getString(key: String!) mean? Are names of operators below correct: ?. : Safe call operator ?: : Elvis operator !! : Sure operator What does differences between ?: operator and ?.let ? When should I use each one? 回答1: The first one that is the single exclamation mark (!) is called the

Confused about Null Safety in Kotlin

这一生的挚爱 提交于 2020-11-27 01:58:09
问题 I am new to Kotlin and have read more than one time about Null Safety in Kotlin but I am really confused and do not understand clearly. Can anyone helps me answer questions below: What does ! character in fun getString(key: String!) mean? Are names of operators below correct: ?. : Safe call operator ?: : Elvis operator !! : Sure operator What does differences between ?: operator and ?.let ? When should I use each one? 回答1: The first one that is the single exclamation mark (!) is called the

Confused about Null Safety in Kotlin

隐身守侯 提交于 2020-11-27 01:57:37
问题 I am new to Kotlin and have read more than one time about Null Safety in Kotlin but I am really confused and do not understand clearly. Can anyone helps me answer questions below: What does ! character in fun getString(key: String!) mean? Are names of operators below correct: ?. : Safe call operator ?: : Elvis operator !! : Sure operator What does differences between ?: operator and ?.let ? When should I use each one? 回答1: The first one that is the single exclamation mark (!) is called the

Null safety in legacy Java libraries used in Kotlin projects

雨燕双飞 提交于 2020-02-03 10:57:48
问题 Let's say I have particular code in old/legacy Java library: public class JavaClass { private String notNullString; private String nullableString; private String unannotatedString; public JavaClass(@NotNull String notNullString, @Nullable String nullableString, String unannotatedString) { this.notNullString = notNullString; this.nullableString = nullableString; this.unannotatedString = unannotatedString; } @NotNull public String getNotNullString() { return notNullString; } @Nullable public

Only safe or non null assserted calls are allowed on a nullable receiver type of arraylist

☆樱花仙子☆ 提交于 2019-12-10 09:44:57
问题 Just started using kotlin for android development.My arraylist is declared like this- var day1: ArrayList<DietPlanDetailModel>? = null Now I am trying to access an element by its position val dietPlan= day1[position] but i am getting below compile time error- Only safe or non null assserted calls are allowed on a nullable receiver type of arraylist Why am i getting this error and how can i resolve it? 回答1: The problem is, that you defined the ArrayList as nullable . You have two options here:

Kotlin call function only if all arguments are not null

China☆狼群 提交于 2019-12-09 02:46:22
问题 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,

Only safe or non null assserted calls are allowed on a nullable receiver type of arraylist

别来无恙 提交于 2019-12-05 15:55:20
Just started using kotlin for android development.My arraylist is declared like this- var day1: ArrayList<DietPlanDetailModel>? = null Now I am trying to access an element by its position val dietPlan= day1[position] but i am getting below compile time error- Only safe or non null assserted calls are allowed on a nullable receiver type of arraylist Why am i getting this error and how can i resolve it? The problem is, that you defined the ArrayList as nullable . You have two options here: 1) don't define the variable nullable (this depends on your code): var day1: ArrayList<DietPlanDetailModel>

Kotlin not nullable value can be null?

陌路散爱 提交于 2019-12-05 15:04:06
问题 I have backend that return me some json. I parse it to my class: class SomeData( @SerializedName("user_name") val name: String, @SerializedName("user_city") val city: String, var notNullableValue: String) Use gson converter factory: Retrofit retrofit = new Retrofit.Builder() .baseUrl(ENDPOINT) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); And in my interface: interface MyAPI { @GET("get_data")

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

喜夏-厌秋 提交于 2019-12-04 20:32:00
问题 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

Kotlin not nullable value can be null?

本小妞迷上赌 提交于 2019-12-04 00:37:51
I have backend that return me some json. I parse it to my class: class SomeData( @SerializedName("user_name") val name: String, @SerializedName("user_city") val city: String, var notNullableValue: String) Use gson converter factory: Retrofit retrofit = new Retrofit.Builder() .baseUrl(ENDPOINT) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); And in my interface: interface MyAPI { @GET("get_data") Observable<List<SomeData>> getSomeData(); } Then I retrieve data from the server (with rxJava) without