0

I am learning Kotlin. and I am working on fundamentals. let's consider the code in the screenshot:

enter image description here

I am having really a hard time reading the message it is displaying and comprehending it.

1: I know it is related to the need to having a total order between the elements of the array so that it would make sense to use binary search.

2:I also know that if I use an IntArray, I don't need to specify the comparator since Integers have their defined total order.

but could someone please explain what is <out TypeVariable(T)> and how to read the following in a coherent sentence :

 fun Array.binarySearch(element: T, comparator: kotlin.Comparator / = java.util.Comparator /, fromIndex: Int = ..., toIndex: Int = ...): Int defined in kotlin.collections

also could you give me an example of an array of objects and a portion of code that defines a total order for them (not the usual ones: > or alphabetical...)

Mufasa
  • 63
  • 5
  • I think the message is just because you haven't typed in what the comparator is. – user253751 Feb 25 '23 at 21:42
  • yes. I understand. but I am trying to become good at reading those messages and understanding them thoroughly. it would be great to no what is at least :'D – Mufasa Feb 25 '23 at 21:43

1 Answers1

2

Well, <out TypeVariable(T)> is related to some advanced concepts such as generics and other things related to it, as you still learning kotlin fundementals, it is very OK you could not understand this message for now.

In general, the reason behind this message is that you didn't specify the element you are searching for, there is a parameter called element should be there, the type of it is T, T here means a generic type, it accepts any class whether Int, String, anything, but it make since to put Int type in the parameter because your Array consist of Integers, otherwise en exception will be thrown.

For the Comperator, you can set it with the value compareBy { it } to compare the integers, and of course using IntArray is better in your case, more efficient, and simpler. So, use Array type only for non-primitive types. The reason behind specifying Comperator shines when you have a list of objects and you want to compare based on a property of these objects. But for Int type, use IntArray and everything will be good.

What is 'out' keyword?

Documentation:

https://kotlinlang.org/docs/generics.html#type-projections

Another question in stackoverflow about it:

What is out keyword in kotlin

What is the mean of compareBy { it } above ?

This function is used to specify the property you want to use to compare the array elements, since your Array is of Int type, it here is Int type, it means compare array elements by integer values.

To make it clearer, let us create a simple class called Book:

class Book(
    val title: String,
    val pages: Int
)

Suppose you have an array of books that are sorted according to the pages number, and you want to do a binary search, here we are using compareBy to tell the compiler to use pages number to compare the books, so it performs the comparisons properly:

    val book1 = Book("Learn Kotlin", 100)
    val book2 = Book("Learn Java", 200)
    val books = arrayOf(book1, book2)
val index = books.binarySearch(element = book2, comparator =  compareBy { it.pages }, fromIndex =  0, toIndex = books.size)


println(index) //prints 1

Look at compareBy { it.pages }, it is of type Book, and we are specifying .pages to compare books based on their pages.

  • I am familiar with generics, and I understood what you said. but what is "out" in – Mufasa Feb 25 '23 at 22:44
  • Ok, I got it, I will forward you to a link to the documentation that explains in detail what is 'out', I will update the answer – Abdulrahman Bahaml Feb 25 '23 at 22:48
  • thank you for the explanation. I am closer to nailing this. but here is the thing `compareBy { it } ` "it" is only one parameter while comparisons in general need 2 parameters to tell you which one of the 2 to pick/ return based on the cdt (that is the comparison between the two) ....or is "it" the index on the collection of elements and you do the it and it+1 thing – Mufasa Feb 25 '23 at 22:56
  • 1
    @Mufasa I added an example that explains `compareBy` – Abdulrahman Bahaml Feb 25 '23 at 23:41