1
votes

I have watched a few tutorials on learning Kotlin, and I am confused as to the meaning of the word 'completion' as used in the quote below. The speaker is a developer at JetBrains, and this is taken from a transcript (not youtube subtitles):

Here, we define an extension function to string called lastChar, and we can use it as a member function. It's visible in completion, so it can be easily discovered. It's like a regular utility function defined outside of the class. But on the other hand, thanks to completion, it can be easily found and used like it was a member.

What is the meaning of the word completion in this usage?

Thank you.

3
Completion means the suggestions to complete the code you're typing given by the IDE. so, if there's a function called getData, and you type get, getData will be shown in the list of code completion appears below the cursor to quickly complete your code. For example - see this image.Lalit Fauzdar

3 Answers

1
votes

They mean "autocompletion", as in it will be shown when you put a . after an instance of the class being extended.

For example, typing "myString". will suggest the following extension function if it exists:

fun String.lastChar(): Char {
    return this.last()
}

However it would not suggest the following non-extension function:

fun lastChar(value: String): Char {
    return value.last()
}
1
votes

Once you type . after an object, you can see all its member and extension functions and properties in the auto-completion box. Their point is that an extension makes this possible for what would otherwise be a utility function that you would have to remember by name.

enter image description here

1
votes

Given the following code:

class X()

fun X.aaaa() {
    
}

val foo = X()

foo.aa

When you position the cursor after foo and type .aa in IntelliJ, you'll see the following context menu showing you that a method aaaa exists on the foo instance. You can press Return at this point to "autocomplete" the reference to that method.

[1]: https://i.stack.imgur.com/e2EaY.png
[2]: https://i.stack.imgur.com/IsQCv.png

Note that aaaa() is offered as an option (the only option)...as the one method available to you on your foo instance that starts with aa. That's what it's talking about. aaaa() is shown as an available instance method even though it is defined as an extension function outside of the definition of X itself.