0

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.