11
$\begingroup$

Many languages have some form of expressing "user-defined duck typing": defining a type by its behavior, rather than anything about the structure or data of an instance of the type itself. Haskell and Lean have typeclasses. Java, C#, Go, Julia, TypeScript, Idris, and many more have interfaces. Swift has protocols. Rust and Scala have traits. C++ and Nim have concepts. Standard ML, OCaml, and Agda have modules.

All of these allow for expressing about the same thing in their respective implementations. Which of these are exact synonyms? What are differing traits (heh) between these different concepts (heh)? Semantics vary by language, of course: but are there any broadly agreed-upon differences between a "trait" and a "typeclass", for example? Modules (and classes, though classes are often complemented with interfaces instead of allowing multiple inheritance) are more broadly distinct from the others, but what about the others?

I am fairly aware of the specifics in implementation between different languages and am not looking for that as an answer, unless the broad answer to this question is "there are no name-associated conventions" in which case, so be it. (though if someone was particularly inclined to go through and write a comprehensive guide about the differences in individual languages' implementations, that'd certainly also be useful).

$\endgroup$
2
  • 3
    $\begingroup$ Interfaces in Java are not duck typing. A class which has the required methods with the required signatures is still not assignable to the interface type if the class doesn't declare that it implements that interface. The same is generally true in any object-oriented language with nominal (as opposed to structural) types. The generalised term for a type defined by its behaviour rather than its memory representation, is an abstract data type. $\endgroup$
    – kaya3
    Aug 28, 2023 at 20:56
  • $\begingroup$ Do note that ADTs can be perfectly emulated in Java these days using interfaces $\endgroup$
    – Seggan
    Aug 28, 2023 at 23:32

1 Answer 1

4
$\begingroup$

No.

Computer science as a whole suffers from conflicting and incoherently defined terminology. There is no language-independent distinction between traits, and interfaces, and typeclasses, and protocols.

That being said: arguably there's no language-independent consistent definition of even a function (pure? total?)... and words are based on usage. In academia, these terms more often than not refer to the particular systems that implemented them: for example, How to make ad-hoc polymorphism less ad-hoc (typeclasses) and An approach to multiple-inheritance subclassing (traits). In the wild, "typeclasses" typically refer to Haskell typeclasses: "interfaces" typically refer to Java interfaces: "traits" typically refer to Rust traits: "protocols" presumably refer to one-of Swift or Elixir protocols: "concepts" presumably refer to one-of C++ or Nim concepts. Modules typically refer to an ML implementation.

(this answer has been rewritten from a previous self-answer addressing a less focused question)

$\endgroup$
8
  • 1
    $\begingroup$ Your question could be helpful to someone looking for the difference between all these terms, but typeclasses, traits, interfaces, and protocols are not the same thing. Interfaces in Java are more like classes with multiple inheritance than anything else. $\endgroup$
    – user
    Aug 28, 2023 at 23:17
  • 1
    $\begingroup$ Also, modules don't really do the same thing as interfaces. Of course, all the different concepts you've listed here are different ways of solving the same problems, but modules are kinda like typeclasses except with the instance not being implicitly provided. $\endgroup$
    – user
    Aug 28, 2023 at 23:24
  • $\begingroup$ Interfaces in Java, sure. But other languages have interfaces too! I'm interested if there is a consistent distinction between interfaces, typeclasses, traits, protocols that isn't tied to particular language implementations. Or when implementing a similar feature do language designers simply reach into a bag and pull out a random name? Since there is this distinction between interfaces, concepts, and modules... $\endgroup$
    – apropos
    Aug 28, 2023 at 23:30
  • $\begingroup$ Right, I'm saying that the phrase "approximately synonyms" makes it seem like they're all the same thing, which they're not. By the way, we're discussing this question in chat if you want to join. Michael Homer suggested splitting this question up so it isn't comparing every one of those things at once $\endgroup$
    – user
    Aug 28, 2023 at 23:49
  • $\begingroup$ Neat, I'll drop by. $\endgroup$
    – apropos
    Aug 28, 2023 at 23:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .