Method invocations of trait-bound generics need explicit upcasting #6059
Labels
during 2: compilation of correct program
Dafny rejects a valid program during compilation
has-workaround: yes
There is a known workaround
kind: enhancement
Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny
part: language definition
Relating to the Dafny language definition itself
part: resolver
Resolution and typechecking
Summary
Dafny currently does not allow direct invocation of methods that are defined in a trait, if the invocation is on a generic type bound by that trait:
Compiling this code would yield the error
member 'foo' does not exist in type parameter 'U'
.Background and Motivation
Even though U is a subtype of T, the compiler does not allow direct invocation of
foo
ont
.To work around this, we need an explicit cast:
This behavior is unintuitive and adds unnecessary verbosity.
Proposed Feature
Enhance Dafny's support for bounded polymorphism by allowing direct invocation of methods on trait-bound generics without requiring upcasting. For the example above,
t.foo()
should compile without errors.Alternatives
Update the documentation to explain why explicit upcasting in this scenario is required. This would reduce confusion but not resolve the underlying issue of verbosity and usability.
The text was updated successfully, but these errors were encountered: