lapots
lapots

Reputation: 13395

cannot resolve reference to methods

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

Upvotes: 0

Views: 377

Answers (1)

Kiskae
Kiskae

Reputation: 25573

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

Upvotes: 2

Related Questions