0Day Forums
Kotlin extension functions vs member functions? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Kotlin (https://0day.red/Forum-Kotlin)
+--- Thread: Kotlin extension functions vs member functions? (/Thread-Kotlin-extension-functions-vs-member-functions)



Kotlin extension functions vs member functions? - hanspt - 07-20-2023

I am aware that extension functions are used in Kotlin to extend the functionality of a class (for example, one from a library or API).

However, is there any advantage, in terms of code readability/structure, by using extension functions:

class Foo { ... }

fun Foo.bar() {
// Some stuff
}

As opposed to member functions:

class Foo {

...

fun bar() {
// Some stuff
}
}

?

Is there a recommended practice?


RE: Kotlin extension functions vs member functions? - naamann848 - 07-20-2023

# When to use member functions
You should use member functions if all of the following apply:

- The code is written originally in Kotlin
- You can modify the code
- The method makes sense to be able to use from any other code

# When to use extension functions

You should use extension functions if any of the following apply:

- The code was originally written in Java and you want to add methods written in Kotlin
- You cannot change the original code
- You want a special function that only makes sense for a particular part of the code

# Why?

Generally, member functions are easier to find than extension functions, as they are guaranteed to be in the class they are a member of (or a super class/interface).

They also do not need to be imported into all of the code that uses them.


RE: Kotlin extension functions vs member functions? - proprioceptive185487 - 07-20-2023

From my point of view, there are two compelling reasons to use extension functions:

1. To "extend" the behaviour of a class you're not the author of / can't change (and where inheritance doesn't make sense or isn't possible).

2. To provide a scope for particular functionality. For example, an extension function may be declared as a freestanding function, in which case it's usable everywhere. Or you may choose to declare it as a (private) member function of another class, in which case it's only usable from inside that class.

It sounds like #1 isn't a concern in your case, so it's really more down to #2.


RE: Kotlin extension functions vs member functions? - coholdersofbmpjv - 07-20-2023

Extension functions are similar to those you create as a utility functions.

A basic example would be something like this:

// Strings.kt
fun String.isEmail() : Boolean {
// check for email pattern and return true/false
}

This code can be written as a utility function in Java like this:

class StringUtils {
public static boolean isEmail(String email) {
// check for email pattern and return true/false
}
}

So what it essentially does is, calling the same function with the object you call on will be passed as the first parameter to the argument. Like the same function I have given example of in `Java`.

If you want to call the extension function created in `kotlin` from `java`, you need to pass the caller as the first argument. Like,

StringsKt.isEmail("[email protected]")

As per the documentation,
> Extensions do not actually modify classes they extend. By defining an extension, you do not insert new members into a class, but merely make new functions callable with the dot-notation on variables of this type.

They are simply static functions with the caller as the first argument and other parameters followed by it. It just extends the ability for us to write it that way.

### When to create extension functions?

* When you don't have access to that class. When that class belongs to some library you have not created.
* For primitive types. Int, Float, String, etc.

The another reason for using extension function is, you don't have to extend that class in order to use the methods, as if they belong to that class (but not actually part of that class).

Hope it makes a bit clear for you..


RE: Kotlin extension functions vs member functions? - elyzak - 07-20-2023

As mentioned in other answers, extension functions are primarily used in code that you can't change - maybe you want to change complex expression around some library object into easier and more readable expression.

My take would be to use extension functions for `data classes`. My reasoning is purely philosophical, `data classes` should be used only as data carriers, they shouldn't carry state and by themselves shouldn't do anything. That's why I think you should use extension function in case you need to write a function around `data class`.