logoalt Hacker News

zdimensiontoday at 10:57 AM1 replyview on HN

Eli5:

Haskell type classes are not classes (like Java or PHP classes); they are comparable to Rust traits -- which are different from PHP traits which are comparable to Java/C# interfaces (with default impls; if you just want contracts you have... PHP interfaces).

A fundamental difference is that you can instantiate/implement a type class (or Rust trait) for any* type, compared to interfaces where each class declares the interfaces it implements. You can therefore create generic (forall) instances, higher kinded type classes, etc.


Replies

pjmlptoday at 11:19 AM

That conflates type classes with extension types, in type theory.

Actually in modern Java you can simulate type classes approach with a mix of interfaces and default methods implementations.

In C# you can have the experience more straightforward with extensions types introduced in C#13.

Then we have yet another way to approach type classes in Scala, with traits and implicits.

And so on, as I haven't yet run out of examples.

show 1 reply