I need help understanding interface. In the documentation I read it is not possible to create instance from interface. But MutableStateFlow is an interface. In the documentation it shows instance is created “ An instance of MutableStateFlow with the given initial value can be created using MutableStateFlow(value) constructor function.” I am assuming this SAM. Single Abstract Method (SAM) interface. Please suggest me documentation so I can clarify this Thank you
Just ctrl+click on the code in IntelliJ and you will know the answer. There is a MutableStateFlow interface and MutableStateFlow function. MutableStateFlow(value) doesn’t create an instance of the interface, it calls the function and the function returns one of implementations (StateFlowImpl).
@broot gave already the correct answer, I just want to point out that there are several ways to “fake” an instance creation. Using a function of the same name like in your case is one way, but you can also use companion objects, e.g.:
interface Test {
fun greet() : String
companion object {
operator fun invoke() = object : Test {
override fun greet(): String = "Hello world!"
}
}
}
val t = Test()
This can be quite confusing, so I can only repeat the advice to check the code when in doubt.