I have some abstract with generic type:
interface Abstract<T> {
val dataHolder: T
}
class AbstractImplA : Abstract<String> {
override val dataHolder = "Test"
}
class AbstractImplB : Abstract<Int> {
override val dataHolder = 1
}
Now I want to create open class that persist this data:
open class Base<T>(private val abstract: Abstract<T>) {
val data : T get() = abstract.dataHolder
// ...
}
now for child class I have to explicitly specifiy generic type of Abstract:
class ClassA : Base<String>(AbstractImplA()) {
fun foo() {
assertEquals("Test", data)
}
}
class ClassB : Base<Int>(AbstractImplB()) {
fun foo() {
assertEquals(1, data)
}
}
I wander if Kotlin can infere this type.
Passing wrong generic type causes Type mismatch error ex. class ClassB : Base<Boolean>(AbstractImplB())
Can I somehow write it like this?:
class ClassB : Base(AbstractImplB()) {
fun foo() {
assertEquals(1, data)
}
}
Related
Hi I am new to programming and trying to implement MVP pattern by passing generic Presenter class LoginPresenter to Generic Model Class LoginUserModel but getting type mismatch error.
on loginUserModel.onAttach(this)
and I am unable to figure out how to pass pass generic interface to another class.
Login Presenter
class LoginPresenter<V : ILoginView>: BasePresenter<V>(), ILoginPresenter<V> {
lateinit var loginUserModel: LoginUserModel<ILoginPresenter<ILoginView>>
lateinit var iLoginPresenter: ILoginPresenter<V>
.........
.........
override fun setupModel() {
iLoginPresenter = this
loginUserModel = LoginUserModel()
// here i am getting error
/**
Type mismatch.
Required:
ILoginPresenter<ILoginView>
Found:
LoginPresenter<V>
*/
loginUserModel.onAttach(this)
}
}
Login Model
class LoginUserModel<P: ILoginPresenter<ILoginView>> : LoginModelContract<P> {
var iLoginPresenter : P? = null
override fun onAttach(ILoginPresenter: P) {
iLoginPresenter = ILoginPresenter
}
}
LoginModelContract
public interface LoginModelContract<P: ILoginPresenter<ILoginView>> {
fun getUsersList(
userName:String,
guid: String
)
fun onAttach(ILoginPresenter: P)
fun onDetatch()
fun getPresenter(): P?
}
You can use two generic statements like below
class LoginUserModel<V: ILoginView, P : ILoginPresenter<V>> : LoginModelContract<V,P> {
var iLoginPresenter : P? = null
override fun onAttach(ILoginPresenter: P) {
iLoginPresenter = ILoginPresenter
}
}
interface ILoginView{
}
interface ILoginPresenter<T>{
fun setupModel()
}
class LoginPresenter<V : ILoginView>: ILoginPresenter<V> {
lateinit var loginUserModel: LoginUserModel<V,ILoginPresenter<V>>
lateinit var iLoginPresenter: ILoginPresenter<V>
override fun setupModel() {
iLoginPresenter = this
loginUserModel = LoginUserModel()
loginUserModel.onAttach(this)
}
}
public interface LoginModelContract<V: ILoginView, P : ILoginPresenter<V>> {
fun onAttach(ILoginPresenter: P)
}
In java, i hava code like below:
public class GenericTest {
private static final GenericTest instance = new GenericTest();
IRequirement requirement;
public static GenericTest getInstance() {
return instance;
}
public class ClassA {}
public interface InterfaceA {}
public void init(IRequirement requirement){
this.requirement = requirement;
}
public interface IRequirement {
<T extends ClassA & InterfaceA> T supply();
}
class ClassB {
void doSomeThing() {
ClassA a = requirement.supply();
InterfaceA ia = requirement.supply();
}
}
}
I can get ClassA or InterfaceA instance according to my needs.
But in kotlin, same codes like below:
open class ClassA(val name: String) {
fun function1() {}
fun function2() {}
}
interface InterfaceA {
fun iFunction1()
}
class ModuleX private constructor() {
var requirement: IRequirement? = null
companion object {
val instance = ModuleX()
}
fun init(requirement: IRequirement) {
instance.requirement = requirement
}
interface IRequirement {
fun <T> supply(): T where T : ClassA, T : InterfaceA
}
}
object ClassB {
inline fun <reified T> doSomeThing() where T : ClassA, T : InterfaceA{
val require = ModuleX.instance.requirement?.supply<T>()
require?.function1()
require?.iFunction1()
}
fun doAnotherThing() {
// IDE give an error when calling supply()
val require = ModuleX.instance.requirement?.supply()
require?.function1()
require?.iFunction1()
}
}
I must specify the actual type of the generic, or use the generic method, otherwise I will not be able to use the IRequirement in the code above, such as in ClassB.doAnotherThing() where IDE give an error "Type inference failed: Not enough information to infer parameter T in fun supply( ): T where T : InterfaceAPlease specify it explicitly."
My question is: In my module, it is required to provide a class that extends ClassA and implements InterfaceAd, but the module does not know the exact type of the class because it is outside the module. How should I use generics in this case?
fun doAnotherThing() {
val require: Any? = ModuleX.instance.requirement?.supply()
if (require != null) {
(require as ClassA).function1()
(require as InterfaceA).iFunction1()
}
}
class ModelFactory {
fun setA() : ModelFactory {
// blabla...
}
fun setB() : ModelFactory {
// blabla...
}
fun setC() : ModelFactory {
// blabla...
}
fun build() : Model {
// An error occurs if any of setA, setB, and setC is not called.
}
}
//example
fun successTest() {
ModelFactory().setA().setB().setC().build() // No error occurs at compile time
}
fun failTest() {
ModelFactory().setA().build() // An error occurs at compile time because setB and setC are not called.
}
It's awkward grammatically, but I think it's been expressed what I want.
I have already implemented an error-raising runtime for this requirement, but I want to check this at compile time.
If possible, I think I should use annotations. But is this really possible at compile time?
With Kotlin, I have been avoiding builder pattern, as we can always specify default values for non-mandatory fields.
If you still want to use a builder pattern, you can use Step builder pattern that expects all mandatory fields to be set before creating the object. Note that each setter method returns the reference of next setter interface. You can have multiple Step builders based on the combination of mandatory fields.
class Model(val a: String = "", val b: String = "", val c: String = "")
class StepBuilder {
companion object {
fun builder(): AStep = Steps()
}
interface AStep {
fun setA(a: String): BStep
}
interface BStep {
fun setB(b: String): CStep
}
interface CStep {
fun setC(c: String): BuildStep
}
interface BuildStep {
//fun setOptionalField(x: String): BuildStep
fun build(): Model
}
class Steps : AStep, BStep, CStep, BuildStep {
private lateinit var a: String
private lateinit var b: String
private lateinit var c: String
override fun setA(a: String): BStep {
this.a = a
return this
}
override fun setB(b: String): CStep {
this.b = b
return this
}
override fun setC(c: String): BuildStep {
this.c = c
return this
}
override fun build() = Model(a, b , c)
}
}
fun main() {
// cannot build until you call all three setters
val model = StepBuilder.builder().setA("A").setB("B").setC("C").build()
}
I've got interfaces
interface IIMSIdentifiable {
fun setImsId(id : String)
fun getImsId() : String
}
interface IIMSConsumer : IIMSIdentifiable {
fun consumeAsync(message : GRLMessage)
}
And I've got a class that contains object with IIMSConsumer type
class IMSObject<IIMSConsumer> : Thread {
constructor(component : IIMSConsumer) {
obj = component
// IMSContext.instance.registerObject(this) // hm type mismatch
}
val objectMessageQueue = LinkedBlockingDeque<GRLMessage>()
val obj : IIMSConsumer
var isRunning = true
override fun run() {
while(isRunning) {
processMessages()
}
}
fun stopProcessing() {
isRunning = false
}
fun processMessages() {
objectMessageQueue.forEach {
obj.consumeAsync(it)
}
}
fun getObjectId() : String {
return obj.getImsId()
}
}
But it cannot resolve references
fun processMessages() {
objectMessageQueue.forEach {
obj.consumeAsync(it) // cannot resolve reference !!!
}
}
fun getObjectId() : String {
return obj.getImsId() // cannot resolve reference !!!
}
What is the problem? Oddly enought it did not ask for imports despite being located in different packages
com.lapots.game.journey.ims.domain.IMSObject
com.lapots.game.journey.ims.api.IIMSConsumer
I tried to test in on something simpler and get the same error with unresolved reference
interface IConsumer {
fun consume() : String
}
class Generic<IConsumer>(val consumer : IConsumer) {
fun invoke() {
print(consumer.consume()) // unresolved reference
}
}
fun main(args: Array<String>) {
val consumer = object : IConsumer {
override fun consume() : String {
return "I consume"
}
}
val generic = Generic<IConsumer>(consumer)
generic.invoke()
}
class Generic<IConsumer>(val consumer : IConsumer) {
You are creating a class Generic with a generic type parameter called IConsumer. This type parameter will shadow the interface you defined within that class, so you it actually says is:
class Generic<IConsumer : Any>(val consumer : Any) {
That is why it cannot resolve the method, since the generic parameter can only be interpreted as Any.
To fix either change the type parameter to have the appropriate names and bound (class Generic<T : IConsumer>(val consumer : T) {) or remove the generics entirely
I would like to access the scope of the calling class when creating an "anonymous inner class"
in Kotlin. What would be the equivalent of Java's OuterScope.this syntax? example :
open class SomeClass {
open fun doSomething() {
// ...
}
}
class MyClass {
fun someFunc() {
object : SomeClass() {
override fun doSomething() {
super<SomeClass>.doSomething()
// Access the outer class context, in Java
// this would be MyClass.this
}
}
}
}
this#MyClass
JFYI:
the same syntax for access to receiver of extension function:
fun MyClass.foo() {
// in some nested thing:
this#foo
//...
}
Kotlin Reference: This expressions
in my case i accessed it like : this#MainActivity
class MainActivity : AppCompatActivity() {
inner class Anon : Observer<PagedList<ApplicationUsers>> {
override fun onChanged(pagedList: PagedList<ApplicationUsers>?) {
Toast.makeText(this#MainActivity, "hello", Toast.LENGTH_SHORT).show()
}
}
}