4 ms·
> I also never seen any language doing interfaces like Go. OCaml has used structural subtyping since the beginning for its object layer. C++'s templates also u
by masklinn 14y ago
> I also never seen any language doing interfaces like Go.
OCaml has used structural subtyping since the beginning for its object layer. C++'s templates also use structural subtyping on type arguments. Pierce also covers the subject in TAPL.
> I find that this is pretty impressive as a feature on its own.
There are advantages and inconvenients to structural subtyping (compared to nominative): it's more flexible and has much lower overhead, but it's subject to false positives (structurally equivalent but semantically unrelated objects) and tends to have much worse error reporting. Not to mention structurally typed systems still usually have and need nominative types in their core, for "non-object" types.
- hasenj 14y agoI don't know what structural subtyping is, but I no that Go's interfaces are nothing like anything in C++
- dan00 14y agoclass A { public: int do() { return 1; } }; class B { public: int do() { return 2; } }; template <typename T> int do(T t) { return t.do(); }
- Olreich 14y agoHow does one use that template? Can you show me a function that takes anything with a do() method? Like this?: do<A>(a) do<B>(b) Can I take any arbitrary type with an "int do()" method and use that with do? Like: do(Arbitrary)
- dan00 14y ago"How does one use that template? Can you show me a function that takes anything with a do() method? Like this?:" It should work without the template parameter: do(a), do(b) "Can I take any arbitrary type with an "int do()" method and use that with do?" Yes.
- Olreich 14y agocool
- masklinn 14y agoAs dan00 noted, it works without the template as long as there is no ambiguity. If there's an ambiguity (a template function defined on <int, int> and <double, double> to which you provide an int and a double) then you have to use explicit template parameter to specify which overload will be used. It's not the case here, so it just works.
- masklinn 14y ago> I don't know what structural subtyping is Which is why "you have never seen a language doing interfaces like Go", everybody else calls it what it is (structural typing/structural subtyping) > I no that Go's interfaces are nothing like anything in C++ Well you may "know" it, but your knowledge is wrong.