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);
}