Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

kotlin - How to use multi-generics in modules?

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?

question from:https://stackoverflow.com/questions/65559809/how-to-use-multi-generics-in-modules

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
fun doAnotherThing() {
    val require: Any? = ModuleX.instance.requirement?.supply()
    if (require != null) {
        (require as ClassA).function1()
        (require as InterfaceA).iFunction1()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...