Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 239 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Kotlin extension functions vs member functions?

#1
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?
Reply

#2
# 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.
Reply

#3
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.
Reply

#4
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..
Reply

#5
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`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through