Open cpitclaudel opened 2 years ago
This has been on my mind as I worked on prototype standard library code for enumeration too (https://github.com/dafny-lang/libraries/pull/37). It's a really common pattern in multiple languages to support chaining operations on values (e.g. something like myList.stream().filter(x => x < 5).map(x => x + 2).collect(toList())
in Java), and this feature would be one possible way to support that style without needing to define all possible transformations directly on the type in question.
I think you'd need a bit more than this feature for this, because this issue assumes that there's a new type alias that you're add methods to. In contrast, I think you want to add member methods to existing types, which C# calls "extension methods"?
I agree that general-purpose extension methods would take care of the problem above nicely. For your issue of streaming APIs, though, do you want extension methods or do you want a trait? Presumably you want to attach a consistent spec to all .map
implementations?
One obstacle to reusing existing library types in client code is that Dafny currently does not support defining member functions on type aliases. Here is what I would like to be able to write:
Here is the working Dafny code that I have to write instead:
The status quo makes it harder to reuse existing types from Dafny's standard library, because newly defined methods look "second-class" (they don't get member syntax)
Could we allow member methods on type aliases?