dotty-feature-requests icon indicating copy to clipboard operation
dotty-feature-requests copied to clipboard

Filter/require exports to match the members of a type

Open er2 opened this issue 4 years ago • 2 comments

When I first learned of the new export feature, I imagined a more disciplined version of Groovy's @delegate feature. The use case that jumped to mind is having a class which is functionally similar to a particular data structure, but with some domain specific enhancements. So you want to extend a collection interface and delegate it to a field but not be bound to the specific implementation, nor do you want to reimplement anything.

class Books extends Collection[Book] {

   private val books: List[Book]

   export Collection[Book] from books

   def authors: Set[Author] = books.map(_.author).toSet

}

I'd think this could be implemented as sugar on what currently exists. Picking a syntax might be harder at this point.

er2 avatar Aug 30 '20 04:08 er2

so in this case Collection[Book] has some subset of the List API?

And does this impose an error if not all definitions can be satisfied?

bishabosha avatar Nov 02 '20 15:11 bishabosha

so in this case Collection[Book] has some subset of the List API?

Yes. I'm more familiar with Java where List and Set both implement Collection. I assumed that was so in Scala also. Maybe a slightly better example is you start using a Set and then later realize you need to allow duplicates, so you migrate to a List. But since you declared Collection, your API consumers haven't assumed set or list behavior for handling duplicates.

And does this impose an error if not all definitions can be satisfied?

Yes, a compile error.

er2 avatar Nov 02 '20 17:11 er2