fix: report error when generic method attempts to implement non-generic interface method#3007
Conversation
…ic interface method
6463821 to
c1f84f8
Compare
|
The only thing that could be improved is to add a new error specific to AS. Such errors: isn’t very informative, since the method is actually defined and has the correct signature. The only issue is incorrect parametric polymorphism. Perhaps may sense to add new AS241 error for this |
| if (numOverrideTypeParams == numBaseTypeArgs) { | ||
| overrideInstance = this.resolveFunction(boundFuncPrototype, baseTypeArgs); | ||
| } |
There was a problem hiding this comment.
should we allow this implement when the generic number are the same? I think it will crash also if the typing mismatched.
IMO, maybe we should forbidden all generic function in interface?
There was a problem hiding this comment.
Hmm, I see only one edge case in this scenario:
interface I {
add<T, U>(x: T, y: U): U;
}
class C2 implements I {
add<U, T>(x: T, y: U): U {
return x;
}
}There was a problem hiding this comment.
Good point. If we follow C++ design:
- template function aka. generic function can't be member in interface
- template function also can't override virtual function in interface.
Shall we do the same?
I would prefer to follow C++ design because generic function in interface is a confused behaviour
But it may break some existing code on user side.
What do maintainers prefer?
There I just referred the hehavior of clang. In clang, a generic(template) function isn't regarded as overriding of virtual interface function, so the interface function is regarded as "unimplemented" |
Fixes #3006 .
Changes proposed in this pull request:
TypeScript allows this because it uses type erasure — generics disappear at runtime, so
foo<T>()andfoo()are the same function. AssemblyScript cannot do the same because it uses monomorphization — each type argument produces a distinct function, and the vtable needs a single fixed entry. This is the same reason C++ forbids template functions from overriding virtual functions.The fix treats a generic method as not satisfying a non-generic interface method, matching C++ semantics.