Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin equivalent of Optional.map and a method reference

Tags:

kotlin

The Java

Consider the following Java:

private void example() {
  Optional<String> foo = ...
  Optional<String> bar = 
      foo.map(this::transform1)
          .map(this::transform2)
}

private String transform1(String s) {
  return s + "!";
}

private String transform2(String s) {
  return "@" + s;
}

Note that the transform methods accept non-Optional values.

Question

Does Kotlin have a built-in mechanism to achieve the same thing when using its nullable/non-nullabe types?

The first attempt

I managed to achieve this effect with the following:

fun <A, B> nullMap(a: A?, mapper: (a: A) -> B?): B? =
  if (a != null) mapper.invoke(a) else null

fun example() {
  val foo: String? = "Space cookies"
  val bar1: String? = nullMap(foo, Example::transform1)
  val bar2: String? = nullMap(bar1, Example::transform2)
}

fun transform(s: String) = s + "!"
fun transform2(s: String) = "@" + s

(Again, we note that for the transform methods, s is a non-nullable String.)

So, my question stated another way is: Does a nullMap equivalent already exist in Kotlin's standard library? And/or: is there a different standard way of doing this?

Finally, can the visually cleaner chaining approach be achieved in Kotlin, rather than either separate assignments (above) or nested function calls? For a moment I considered that you could add nullMap as an extension method on Any, but sometimes the receiver you're trying to invoke a method on is null (so that won't work).

like image 250
Greg Kopff Avatar asked Jun 06 '17 03:06

Greg Kopff


People also ask

Should I use Optional in Kotlin?

Depending on whom you ask, using Optional as the type of a field or return type is bad practice in Java. Fortunately, in Kotlin there is no arguing about it. Using nullable types in properties or as the return type of functions is considered perfectly valid and idiomatic in Kotlin.

What is .map in Kotlin?

Kotlin map is a collection that contains pairs of objects. Map holds the data in the form of pairs which consists of a key and a value. Map keys are unique and the map holds only one value for each key. Kotlin distinguishes between immutable and mutable maps.

Why is Java Optional 8?

So, to overcome this, Java 8 has introduced a new class Optional in java. util package. It can help in writing a neat code without using too many null checks. By using Optional, we can specify alternate values to return or alternate code to run.

How do you access map values in Kotlin?

For retrieving a value from a map, you must provide its key as an argument of the get() function. The shorthand [key] syntax is also supported. If the given key is not found, it returns null .


1 Answers

We use safe call operator:

val foo: Foo? = ...
foo?.toA()?.toB()

Or

foo?.let(this::transform1)?.let(this::transform2)

Kotlin emphasis on null safety. There is an entire chapter in its manual describing related technics.

like image 64
glee8e Avatar answered Nov 24 '22 08:11

glee8e