-2
fun Mono<Something>.update(): Mono<Something2> {
   return flatMap { mySomething ->
      // some stuff
   }.map()
}

How does the above code works?

  1. What does Mono<Something> means before the name of the function/method?
  2. How can it use flatMap without any object in front? I thought flatmap usage is like SomeArray.flatMap{}
  3. what does mySomething refer to in this case?
  4. What's the purpose of doing flatMap then chain it with map?
Slaw
  • 37,820
  • 8
  • 53
  • 80
Harts
  • 4,023
  • 9
  • 54
  • 93
  • I doubt `-->` is a valid operator in Kotlin. Can you please check? – Venkatesh-Prasad Ranganath Mar 03 '20 at 01:54
  • you're right it's typo -> – Harts Mar 03 '20 at 02:12
  • 1
    **(1)** That's how [extension functions](https://kotlinlang.org/docs/reference/extensions.html#extension-functions) are declared. **(2)** It's invoking `flatMap` on the `Mono` instance (i.e. `this`). **(3)** It's the parameter of the [lambda expression](https://kotlinlang.org/docs/reference/lambdas.html#lambda-expressions-and-anonymous-functions). **(4)** I can't tell from the provided code. – Slaw Mar 03 '20 at 02:28

1 Answers1

2
  1. Mono is the type being extended with the definition of an extension function update. See Here.
  2. flatMap is being invoked on the this receiver available withing the extension function.
  3. mySomething is the parameter of the lambda that will be bound to the elements provided by Mono via its implementation of Iterable interface (assuming flatMap has the usual semantics).
  4. In cases such as flatMap { it.foo() }.map { it.toString() }, map allows transformations that cannot be pushed into the transformation performed in flatMap.