Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write in parcer nullable value in kotlin

Backend returns nullable Int? How should I write nullable value?

 date class Foo (var value: Int?){
   constructor(source: Parcel) : this(
     source.readInt()
   )

   override fun writeToParcel(dest: Parcel, flags: Int) {
     dest.writeInt(value) // doesn't compile - writeInt expect no null value
   }
 }

Now I got solution:

dest.writeInt(value?: -1)

And then check to -1

or write Int like string and then use value of...

but I think it's ugly and wrong.

RESOLVED! My answer:

source.readValue(Int::class.java.classLoader) as Int?,
dest.writeValue(value)
like image 814
Yvgen Avatar asked Jan 26 '17 12:01

Yvgen


People also ask

How do you declare nullable in Kotlin?

type can hold either a string or null , whereas a String type can only hold a string. To declare a nullable variable, you need to explicitly add the nullable type. Without the nullable type, the Kotlin compiler infers that it's a non-nullable type.

How do you accept null value in Kotlin?

Kotlin has a safe call operator (?.) to handle null references. This operator executes any action only when the reference has a non-null value. Otherwise, it returns a null value. The safe call operator combines a null check along with a method call in a single expression.

How do I create a non-null variable in Kotlin?

The Elvis operator is used to return a non-null value or a default value when the original variable is null. In other words, if left expression is not null then elvis operator returns it, otherwise it returns the right expression. The right-hand side expression is evaluated only if the left-hand side found to be null.

How do you null an object in Kotlin?

In Kotlin, references to objects cannot contain null values by default. To assign a null value to a variable, you must declare a nullable variable type by adding ? to the end of the base type.


2 Answers

Resolved by:

source.readValue(Int::class.java.classLoader) as Int?,
dest.writeValue(value)
like image 114
Yvgen Avatar answered Oct 19 '22 12:10

Yvgen


This largely depends on what is the semantics of null as a value of your nullable property. It can mean:

  • If value is null then it is absent and should not be written at all:

    value?.let { writeInt(it) }
    

    Of course, the Parcel receiver should be able to determine if it should read the value or not, this should be clear from the values written earlier.

  • value is provided by some code, and it is an error if it is null at the point where it should be written:

    check(value != null) { "value should be not null at the point it's wriiten" }
    writeInt(value!!)
    

    Also, in this case, consider using lateinit var instead of nullable property.

  • Some default value should be used instead of value:

    writeInt(value ?: someDefaultValue())
    

    With Parcel, this does make sense, because otherwise, if the value is missing, you have to specify the fact somewhere else.

  • ... (null can actually mean many things)

Also, this answer shows many idiomatic ways of dealing with nullable values, and you might find some of them useful.

like image 28
hotkey Avatar answered Oct 19 '22 14:10

hotkey