Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin, representing an undefined yet value

So the big thing about Kotlin that it forces you to not implement the null idea in your project.

what is the replacement of the null idea then, if for example you have to create a new object Foo() then fill in it's properties (like filling a form for example) foo.name = "foo", foo.age = 10, etc...
then check which fields have not been assigned with a value and warn the user to fill it in.

i can simply use the ? on the variables types and check if it's null but that's against the whole idea of getting rid of Null Pointer Exception

is there any good implementation of an undefined yet field in Kotlin .

like image 628
Hassan Avatar asked Jan 03 '23 20:01

Hassan


1 Answers

So the big thing about Kotlin that it forces you to not implement the null idea in your project.

No. The "Null Safety" concept in Kotlin does not mean do eliminate all null values. Actually, even exactly the contrary: you can now safely use null values. The idea is to prevent calls on null references at runtime. To achieve this, Kotlin forces you to deal with potential null values at compile time.

i can simply use the ? on the variables types and check if it's null but that's against the whole idea of getting rid of Null Pointer Exception

The architects of Kotlin have put a lot of effort into the concept of "?". The nullable type is intended to define an undefined state. And because it is now safe to use null values, there are concepts like Safe Calls, Elvis Operator or Safe Casts to handle null values efficiently (and not just put if (a != null) around it).

like image 153
Marcel Avatar answered Jan 05 '23 15:01

Marcel