Drocchio
Drocchio

Reputation: 413

why the methods /propreties of an interface can be passed in an extension function

Today I had to use an extension function which had as predicate an Interface, a kind of Interface.whateverExtensionFunction() and a I had a class ViewModel () :InterfaceInherithingFromAnotherInterface. The architectural idea behind is to maintain my viewModel tidy and delegate some heavy weight to the extension function. I do not understand why into my ViewModel in whatever method I can call the extension function FruitColorsInterface.changeColors() as in the code below into the method cut()

I do not undersand how is possible that effectively into the extension function, I can call the interface method

If a class implements an interface this is a contract to implement the methods of the interface and not too pass an object Interface has happens in this extension class**

class testInterface(){

    @Test
    fun testInterface(){
AppleTrim().cut()
    }

}

class AppleTrimViewModel : FruitColorsInterface{
    override val red: String
        get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
    override val green: String
        get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.

    override fun mixColors(): String {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun move3d() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun spinFromTop(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    fun cut(){

        changeColors()
        //FruitColorsInterface.changeColors()//why this is error?

    }
}

interface FruitColorsInterface {
    val red :String
    val green:String

    fun mixColors(): String
    fun move3d()
    fun spinFromTop() :Int

}

fun FruitColorsInterface.changeColors(){
    println("Ehy")
    mixColors()//WHY IS THAT? 

}

if I decompile in Java I get a static function where is passed an interface

    public static final void changeColors(@NotNull FruitColorsInterface $this$changeColors) {
          Intrinsics.checkParameterIsNotNull($this$changeColors, "$this$changeColors");
          String var1 = "Ehy";
          System.out.println(var1);
          $this$changeColors.mixColors();
       }
//and
   public final void cut() {
      Test_interfaceKt.changeColors(this);
   }

Upvotes: 0

Views: 47

Answers (1)

Ilya
Ilya

Reputation: 23115

In the scope of an extension function you have an instance of the type that it extends (in your example it is FruitColorsInterface) available as this — an implicit receiver.

Using that this instance you can invoke other functions and properties available on that type, no matter whether they are members or extensions.

As to why you can call just mixColors() instead of this.mixColors() in the extension function body, it's because this is available implicitly, same as in the member function bodies, and thus it can be omitted.

Upvotes: 1

Related Questions