collections icon indicating copy to clipboard operation
collections copied to clipboard

Adding a ReadableCollection interface

Open stof opened this issue 2 years ago • 2 comments

Currently, the only interface we have is Collection, which has both getter methods and mutator methods. Because of that, its generic type is invariant. In many cases, we might want to write code that only cares about reading from a collection, which could then benefit from having a covariant interface instead. This can be solved by adding a ReadableCollection interface as a parent of Collection, moving all reading methods to the parent interface. That parent interface would use @template-covariant. This way, code that only cares about reading from a collection could use the ReadableCollection type, where passing a ReadableCollection<Child> to a method expecting ReadableCollection<Parent> would be accepted. There would be no BC break for users (the Collection interface itself would still have the same signature). What do you think about that ? I can work on a PR if there is an interest.

stof avatar Mar 15 '22 10:03 stof

I think it would be great! @template-covariant is not inherited by extending interfaces, right?

greg0ire avatar Mar 15 '22 11:03 greg0ire

@template and @template-covariant are never inherited. The child class/interface can define its own generic signature (and must use @implements or @extends to fulfil the parent generic signature)

stof avatar Mar 18 '22 21:03 stof

Closing this as it has been implemented in #322

stof avatar Sep 12 '22 16:09 stof