Does Kotlin has a keyword `None`, like Python? - kotlin

I use python almost everyday. Now I am learning Kotlin. I wonder if there is None in Kotlin so that we can do something like:
v1 = None
if v1 is None:
pass
# then we do something
I did some research, and I found there is none in kotlin-stdlib/kotlin.collection, but that does not seems to be something I am looking for.
If there is such keyword like None in Kotlin, how can it be used? If not, how does Kotlin deal with the situation like the code shown above?

Kotlin's equivalent to Python's None is null.
Note that Kotlin has something called "null safety", so any variables that can receive null must be declared as nullable.

null is the alternative for 'None` (Python) in Kotlin

https://kotlinlang.org/docs/reference/null-safety.html
use the keyword "null" instead. in the documentation there is some examples how to use it. It is slightly different than using None in python but it is not complex if you know some C# I think because it has also this same null safety typing feature

Related

What are the benifits of using Higher order functions in Kotlin?

I am learning about higher order functions[HOF] and lambda in Kotlin.
I checked the Kotlin docs but didn't understand it, I found one benefit of HOF:
You can perform any operations on functions that are possible for other non-function values.
so, What are 'non-functional values'?
and What are those 'operations'?
In a higher order function if a lambda is taking two parameters and returning a value, then can't we just use a function for it?
and what is the real scenario when we have return a function?
I have seen real programs in Kotlin, but I haven't seen any use of lambda or HOF in them.
I want to understand why, else many of the features would just go unused.
It's just a part of the Kotlin syntax that makes it more concise and understandable.
For example, try to imagine this code without using lambdas and HOF like map, filter etc:
val sum = listOfInts.filter{it % 2 == 0}.map{it*it}.sumOf{it % 10}
Type-safe builders is a cool thing too.
These functions are widely used in many libraries and frameworks, like Compose by Google. The first thing I remembered - State hoisting pattern.

Convert java Optional to Kotlin Arrow Option

What would be the best way to convert Java Optional to Arrows Option? I was expected to have something out-of-box, but it's not there. Something like:
fun <T> Optional<T>.toOption(): Option<T> = if (this.isPresent) Some(this.get()) else none()
There is no such function at the moment, but such a contribution would be welcomed!
Arrow however does not recommend using Option unless absolutely necessary. The only use-case being nested nulls, which is the limitation for ReactiveX implementation of RxJava & Project Reactor. Both libraries don't allow null being used for their generic value A in Flowable, Flux, Mono, etc.
Analogue, you cannot use null as an empty signal for generic code in Kotlin. Unless A is constraint to be non-null by using A : Any.
Only in both cases should you use Arrow's Option, otherwise using Kotlin's nullable type is recommended by the Arrow maintainers.

Unresolved reference: addCriteria (Kotlin)

I am trying to write some basic MongoDB custom queries in Kotlin.
I've seen nearly everywhere that people use a method addCriteria:
val query: Query = Query()
query.addCriteria(Criteria.where("field1").exists(true)))
But it seems there is no method addCriteria in Query which I use: org.springframework.data.mongodb.repository.Query
I am very confused. Cannot find any explanations on how to write custom MongoDB queries in Kotlin with Spring Data except using this method.
So, I was very close to the problem's solution. I really used wrong Query, despite it's path looks really logical. I needed this one: org.springframework.data.mongodb.core.query.Query.
I used org.springframework.data.mongodb.repository.Query, which is Query annotation, not the class

Same method for nullable and non-nullable arguments

I'm trying to create two almost-same methods that handle nullable and non-nullable arguments slightly differently:
fun parse(type: Any) : MyObject {
return handleParse(type)
}
fun parse(type: Any?) : MyObject? {
if (type == null)
return null
return handleParse(type)
}
But I get this error in Android Studio:
Platform declaration clash: The following declarations have the same JVM signature
The goal is that it automatically handles nullable and non-nullable values in Kotlin, without me using !! every time I call it on nullable terms.
I've already tried adding the #JvmName("-name") annotation as mentioned in this answer but that doesn't work either. Obviously, I can change the method name to something else as well, but that is just circling around and avoiding the issue altogether.
Hoping there's an easy way to do this or at least a sensible workaround. Would also appreciate the reasoning behind the way things currently work, and why I should or shouldn't do this.
Reason why this doesn't work is simple, Java doesn't have null-safe types, meaning that both methods look completely same to Java, and Kotlin aims to provide as much interoperability with Java as possible.
But if you think a bit more about that there is simply no reason for such feature, as you can see your 2nd method already handles everything properly, with addition of 1 if case, which even if this feature exist would have to exist because compiler would need to know whether value in null or not in other to know which method to call anyway.
Common approach that I have seen so far is adding NotNull suffix to your method, for example in your case it would be parseNotNull in case where you don't allow nullable types, this way even when calling code from Java it is clear that parameter shouldn't be null.

What is the name of Kotlin's !! operator

When in a conversation with other developers, what do I call the !! operator?
In Kotlin, the ?: is called the Elvis operator
These sources don't say what the name of !! is:
http://kotlinlang.org/docs/reference/null-safety.html#the--operator
http://kotlinlang.org/docs/reference/keyword-reference.html
Looking online, the generic term for !! is double bang. Do I use the same for Kotlin even though swift's ! operator is called forced unwrapping (Note: the ! in swift is similar to Kotlin's !!.)
What I'm specifically looking for:
A name that I can verbally call the !! operation that Kotlin developers can understand
A name other than double exclamation or bang bang or double bang
The Kotlin documentation refers to it as the not-null assertion operator.
Personally, I call it the hold my beer operator.
"Kotlin in Action" calls it the not-null assertion operator. We've decided to update the docs to use this term too.
I use to call it "double bang", but actually hold my beer operator does get to the heart of it too (thx #Todd ;-)).
I like to say "non-null asserted call" for things like
a!!.length
that is also how it is shown in the Android studio ALT+ENTER context menu.
Makes sense to me because that sums up what it is actually doing.
I'm using "force-unwrapping" (same as is in Swift).