Static Extension Methods in Kotlin

How do you define a static extension method in Kotlin? Is it possible? I currently have an extension method as shown below.

public fun Uber.doMagic(context: Context) { // ... } 

The above extension can be called on an instance.

 uberInstance.doMagic(context) // Instance method 

but how to make it a static method as shown below.

 Uber.doMagic(context) // Static or class method 
+122
static-methods kotlin
Jan 29 '15 at 8:35
source share
6 answers

To reach Uber.doMagic(context) , you can write an extension for the Uber companion object (requires the declaration of a companion object object):

 class Uber { companion object {} } fun Uber.Companion.doMagic(context: Context) { } 
+114
Nov 22 '15 at 9:12
source share

Here is what the official documentation says:

Kotlin generates static methods for package level functions. Kotlin can also generate static methods for functions defined in named objects or companion objects if you comment these functions as @JvmStatic. For example:

Kotlin Static Methods

 class C { companion object { @JvmStatic fun foo() {} fun bar() {} } } 

Now foo () is static in Java, and bar () is not:

 C.foo(); // works fine C.bar(); // error: not a static method 
+11
Jul 06 '16 at 20:06
source share

You can create a static method using the Companion object, for example:

 class Foo { // ... companion object { public fun bar() { // do anything } } } 

and then you can call it like this:

 class Baz { // ... private fun callBar() { Foo.bar() } } 
+5
Apr 09 '15 at 3:33
source share

I really had this exact question 30 minutes ago, so I started digging around and couldn't find any solution or workaround for this, BUT during the search I found this section on the Kotlinglang website that says:

Note that extensions can be defined with a nullable recipient type. Such extensions can be called on an object variable, even if its value is zero.

So, I had the craziest idea, why not define an extension function with a nullable receiver (without actually using this receiver), and then call it for a null object! I tried it, and it worked pretty well, but it looked so ugly. It was like this:

 (null as Type?).staticFunction(param1, param2) 

So I got around this by creating val in my extension type file of the receiver type with a value of null, and then using it in my other class. So, as an example, here's how I implemented the β€œstatic” extension function for the Navigation class in Android: In my NavigationExtensions.kt file:

 val SNavigation: Navigation? = null fun Navigation?.createNavigateOnClickListener(@IdRes resId: Int, args: Bundle? = null, navOptions: NavOptions? = null, navigationExtras: Navigator.Extras? = null) : (View) -> Unit { //This is just implementation details, don't worry too much about them, just focus on the Navigation? part in the method declaration return { view: View -> view.navigate(resId, args, navOptions, navigationExtras) } } 

In the code that uses this:

 SNavigation.createNavigateOnClickListener(R.id.action_gameWonFragment_to_gameFragment) 

Obviously, this is not a class name, it is just a class type variable that has a null value. This is obviously ugly on the creator side of the extension (because they must create the variable) and on the side of the developer (because they must use the SType format instead of the actual class name), but this is the closest that can be achieved correctly Now compared to real static functions . We hope that the creators of the Kotlin language will answer the created problem and add this function to the language.

+4
Jun 12 '19 at 4:08
source share

We recommend that you take a look at this link. As you can see, you just have to declare the method at the top level of the package (file):

 package strings public fun joinToString(...): String { ... } 

It is equal

 package strings; public class JoinKt { public static String joinToString(...) { ... } } 

Everything is the same with constants. This ad

 val UNIX_LINE_SEPARATOR = "\n" 

equally

 public static final String UNIX_LINE_SEPARATOR = "\n"; 
0
Feb 16 '17 at 2:43 on
source share

I also really love being able to add static extension methods to Kotlin. As a workaround at the moment, I am adding an exntension method for several classes instead of using one static extension method in all of them.

 class Util fun Util.isDeviceOnline(context: Context): Boolean { val connMgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val networkInfo = connMgr.activeNetworkInfo return networkInfo != null && networkInfo.isConnected } fun Activity.isDeviceOnline(context: Context) = { Util().isDeviceOnline(context) } fun OkHttpClient.isDeviceOnline(context: Context) = { Util().isDeviceOnline(context) } 
-3
Mar 08 '16 at 16:43
source share



All Articles