dotty-feature-requests
dotty-feature-requests copied to clipboard
Filter/require exports to match the members of a type
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.
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?
so in this case
Collection[Book]
has some subset of theList
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.