0

For example here is all() in action:

fun Shop.checkAllCustomersAreFrom(city: City): Boolean =
    customers.all { it.city == city }

And here is the equivalent from the kotlin documentation:

inline fun <T> Iterable<T>.all(
    predicate: (T) -> Boolean
): Boolean

Can someone please explain each part of the second code block and why it's written like that?

Apologies if this is a basic question, but if I learn this it will be much easier to read documentation.

Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • Read [Kotlin Extension Functions](https://kotlinlang.org/docs/reference/extensions.html) – Enzokie Mar 10 '19 at 06:11
  • I have and I didn't find it very clear - I'd prefer to have SO users try explain it to hopefully give a more concise answer. – Zorgan Mar 10 '19 at 06:14

2 Answers2

4

Let's break it down, shall we?

inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean
|--1--|-2-|-3-|-----4-----|-5-|----6-----|------7-------|----8----|
  1. This function is inline. This means that its body essentially gets copy-pasted to the call site at compile time, as an optimization measure (used in this case because it has a lambda parameter).
  2. Declares a function.
  3. Type parameter list, this function has one generic type parameter called T.
  4. This is an extension function, and this is its receiver, i.e. the type being extended. This function will be callable on any Iterable<T> as if it was a member function. The Iterable that it was called on can be accessed inside the function's body as this.
  5. The name of the function.
  6. The name of the first and only parameter of the function (if we don't count the receiver, which is technically also a parameter).
  7. The type of the function's parameter. This is a function type, which describes a function that takes a single T parameter, and returns a Boolean. This can be a reference to a regular function that has this signature, but the expectation with collection functions such as this is that most of the time this will be a lambda.
  8. The return type of the function.
zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • Thanks - this is what I needed. The only thing I'm confused about now is `3.` (the first ``). Can you give a practical example of using that type parameter? I completely understand the 2nd `` - it's the type of `Iterable` passed through (e.g. `Int`) - but I'm not sure about the first ``. – Zorgan Mar 10 '19 at 07:44
  • 1
    You need to declare any type parameters that you'll use in your function after the `fun` keyword, otherwise writing down `T` in `Iterable` would refer to a real type literally named `T`. This is how you let the compiler know that the `T` you're using is a generic parameter. – zsmb13 Mar 10 '19 at 08:13
  • The second `` is exactly the practical example use of the first ``, along with `(T) -> Boolean`. – Alexey Romanov Mar 10 '19 at 12:22
1

inline - Take the body of this function and put it where it is being called when compiled instead of calling a function.

fun - function declaration

- generic type called T

Iterable - class we are adding an extension function too. (If it is not inline read static function)

all - name of the function

predicate - parameter named predicate

: (T) -> Boolean - Lambda Type takes a T as a parameter and returns a Boolean. Usually in the form { it == foo }

: Boolean - Returns a Boolean

Ge3ng
  • 1,875
  • 1
  • 24
  • 38