1

I created an extension function,

fun <T> Observable<T>.subscribeWithErrorHandling(onNext: (T) -> Unit ,onError: ((throwable: Throwable) -> Unit)? = null): Subscription {
//doing stuff
}

in kotlin class, I will be able to use it no problem in that way

observable.subscribeWithErrorHandling(...)

Now, I want to use this function in my java class as well. I already see that you can call it statically like :

MyExtensionFile.subscribeWithErrorHandling

But in my case, you need something else since it's a middle of an RX flow. And that is the part I'm stuck with. Does this is even possible? or no way to do something like that from the java code?

Kashif Faraz Shamsi
  • 513
  • 1
  • 7
  • 21
agonist_
  • 4,890
  • 6
  • 32
  • 55
  • Possible duplicate of [Accessing Kotlin extension functions from Java](https://stackoverflow.com/questions/28294509/accessing-kotlin-extension-functions-from-java) – Vince Sep 11 '17 at 05:23
  • @VinceEmigh it's not, it's a different use case. – agonist_ Sep 11 '17 at 05:25
  • Extension functions are compiled into static methods, as mentioned in the linked duplicate. That's the answer. If you're looking for a work-around, please specify it. As the post currently is, both the question and answer match the duplicate (regardless of specifics, similar to NPE questions). It's a waste to answer questions which only vary slightly, without any real difference, as it doesn't add value, only inflates the same Q&A's – Vince Sep 11 '17 at 05:27

2 Answers2

0

Simple answer. No.

Explanation - I believe that's not possible since that would mean you're extending the Observableclass which is abstract. Kotlin extensions add the same functionality by moving out of the inheritance tree and in Java in you're pretty much stuck with inheritance.

So the only option left would be to extend the base Observable class and create your own implementation of the same which I think would be unwanted in your case. A simple solution would be to create a new method just for Java which can take an observable and do the required logic. Or create a custom class with the Observable instance as its instance member and then write the required methods inside it (The standard OOPS way). This code could then be used from Kotlin as well as Java.

EDIT: I believe you already know this but will still point you to this question.

Mustansir Zia
  • 984
  • 10
  • 18
  • If you're aware this question has been asked & answered, please flag the question as duplicate. – Vince Sep 11 '17 at 05:23
  • 1
    I believe he was already aware of the solution provided in the question and what he wanted was something else, to be able to use the method in the Rx flow after he instantiates the `observable`. To call his custom methods on the instance. That is why I answered the way I did. – Mustansir Zia Sep 11 '17 at 05:30
  • 1
    From his post: "*And that is the part I'm stuck with. Does this is even possible?*" From the duplicate: "*What am I doing wrong? Is this even possible?*" - How is it not the same? **We don't want 3 million copies of the same questions**. Mark as duplicate, answer the duplicate if you feel their answers aren't suffice. It's how the site works. – Vince Sep 11 '17 at 05:36
0

Extension functions one of the advantages of kotlin over Java. Coding in kotlin gives you those advantages, but unfortunately that does not mean you can have the kotlin language features in Java code.

Solution: port the module where you wish to use extension functions across to kotlin.

innov8
  • 2,093
  • 2
  • 24
  • 31