samuelgoto/proposal-optional-types

Do we need to explicitly annotate classes implementing interfaces?

samuelgoto opened this issue · 0 comments

(porting comments from @domenic to github issues)

@domenic: challenges with introducing new interface syntax and concepts (especially classes implementing interfaces).

// Examples
interface Bar {
  hello(a: number): number;
}
 
class Foo implements Bar {
  bar(a: number) : number {
    return 0;
  }
}

@domenic: Is this (class Foo implements Bar) really necessary? Why? The type checker can tell what interfaces a class implements without any manual annotation.

@goto: Hum, that's a good point, interfaces being structurally typed. @dimvar help me out here?

@dimvar: It's like TypeScript. If you define explicitly that a class implements an interface, then the type checker checks that it does. If you don't, then you can pass class instances to places that expect the interface, and you would get warnings there. Another benefit of letting a class specify the interfaces it implements is that it's good for documentation.

@domenic: That's a pretty dubious value proposition, IMO, for using up such valuable syntactic space and a keyword. (For example, people have proposed using implements in this position for runtime mixins.)

@dimvar: Letting a class specify that it implements an interface has a few advantages: 1) An author of the library/module can say that class A implements B, and have that checked, even if there is no place in the library where an instance of A is passed to a context that expects a B. 2) When A explicitly says that it implements B, then checking that A is a subtype of B is quick, it's a tag check, otherwise, we have to structurally check it, which involves looking at all properties of B. If the specific word implements is too valuable, we could find some other keyword, or new syntax to express this.

@domenic: Yes, I understood the advantages, I just think they are really marginal for the weight they add to a supposed MVP. (Especially one based on speed in a system which is known to make things slower.)