

Please read them fully before writing that I added incorrect links. Q: Is there any way to fix these problems without removing inline modifier from Optional?ġ Examples include some context. If I remove inline modifier from Optional, the test will pass. ReplaceWith = ReplaceWith("Optional.of(_value)") So I've decided to encapsulate it in inline class Optional: inline class Optional = "Not type-safe, use factory method", The last approach has the best performance, but it's also the most error-prone. On the other hand, the second ones are called nullable references and must be of a nullable type. The first ones are called non-nullable references, and their type must be of a non-nullable type. The special UNINITIALIZED_VALUE for representing the second kind of null - example 1 As opposed to what happens in Java, Kotlin distinguishes between references that cannot hold null and those that can.Holder? where Holder is data class Holder(val element: T) - example 1.There are a few approaches for doing this: For example, I need double nullability, when I want to use T? where T may be a nullable type. I still feel that Option is a better choice for a language in general (I like how it is used in rust std lib), but I should admit that for a language that already has nullable, syntax for it and a lot of utility functions, adding option probably does not make sense.In Kotlin sometimes I have to work with double nullability.

In most of cases syntax is pretty much the same. you want to distinguish cases user not found and user found, but it didn’t enter their age). If ((mayBeA != null) & (mayBeB != null) & (mayBeC != null)) Ī rare case when it’s not true is when you need to have Option> (e.g. In the case of multiple values it’s depends on the additional language features.

If (mayBeValue != null) doSmth(mayBeValue) But as I said, I’m just a n00b and would love to get corrected. I can’t imagine a case where having Option leads to a better code. Kotlin goes to great lengths to make it less harmful but I still don’t like usage of null for representing absent optional value. I’d rather say that null is a bad idea by itself. For example, in Scala there are both null and Option but it seems to me that Option is predominant and null is used mostly for interoperation with Java. Simply put, each type is non-nullable by default, but can be made nullable by appending the question. Moreover, sometimes you have to deal with nullable Option. Kotlin implemented an approach to null-safety that can leverage extra compiler support. It leads to split where some libraries use Option and some use null. I’m a n00b in kotlin, but I’d say that Option is a bad idea for an language having also null.
