You are currently viewing How can I resolve this ambiguity?

How can I resolve this ambiguity?

In library I use there is some base class I have to extend. Let it be Base.

In my code I have 2 classes which extend Base:

open class Record: Base() { ... } open class RecordList: Base(), List<Record> { ... } 

Library also defines 2 overloads of some method, let’s call it method:

fun method(b: Base, ...) { ... } fun method(c: Collection<Base>, ...) { ... } 

Then library does it’s magic which basically results in call along the lines of:

method(recordList, ...) 

where recordList is an instance of RecordList. Since List<E> extends Collection<E> class RecordList therefore is Collection<Record> which in turn makes it Collection<Base>. Given this, recordList matches both overloads of method and therefore there is ambiguity and my program does not compile.

I have no control over what happens in library, all I can change are my classes. Is there any way I can resolve it while keeping RecordList a List or I have to reconsider it’s design (e.g. to include collection as property instead)?

Edit: so it seems there is not much I can do besides changing from implementing List to just having it as property. Thanks to everyone for answers

submitted by /u/4sent4
[link] [comments]