Skip to content

Commit 9de7848

Browse files
add some comparisons
1 parent 44a8541 commit 9de7848

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

README.md

+40-8
Original file line numberDiff line numberDiff line change
@@ -298,30 +298,62 @@ specific details about the proposal.
298298

299299
## Relationship to similar features
300300

301-
### Haskell-style typeclasses
301+
### Haskell type classes
302302

303-
TODO
303+
This proposal was strongly inspired by Haskell's type classes. The conceptual
304+
model is identical aside from the fact that in Haskell the type class instance
305+
(essentially an implicit record) is resolved automatically by the type checker.
306+
For a more Haskell-like calling pattern, one can define functions like
307+
308+
```js
309+
function fmap(fn) {
310+
return function (functor) {
311+
return functor[Functor.fmap](fn);
312+
};
313+
}
314+
```
315+
316+
Similar to how each type in Haskell may only have a single implementation of
317+
each type class (newtypes are used as a workaround), each class in JavaScript
318+
may only have a single implementation of each interface.
319+
320+
Haskell type classes exist only at the type level and not the term level, so they
321+
cannot be passed around as first class values, and any abstraction over them must
322+
be done through type-level programming mechanisms. The interfaces in this proposal
323+
are themselves values which may be passed around as first class citizens.
304324

305325
### Rust traits
306326

307-
TODO
327+
Rust traits are very similar to Haskell type classes. Rust traits have
328+
restrictions on implementations for built-in data structures; no such
329+
restriction exists with this proposal. The `implements` operator in this
330+
proposal would be useful in manually guarding a function in a way that Rust's
331+
trait bounds do. Default methods in Rust traits are equivalent to what we've
332+
called methods in this proposal.
308333

309-
### Java-style interfaces
334+
### Java interfaces
310335

311336
TODO
312337

313-
### Ruby-style mixins
338+
### Ruby mixins
314339

315340
TODO
316341

317342
### ECMAScript `mixin(...)` pattern
318343

319344
```js
320-
class A extends mixin(FeatureA, FeatureB) {}
345+
class A extends mixin(SuperClass, FeatureA, FeatureB) {}
321346
```
322347

323-
TODO
348+
This mixin pattern usually ends up creating one or more intermediate prototype
349+
objects which sit between the class and its superclass on the prototype chain.
350+
In contrast, this proposal works by copying the inherited interface methods
351+
into the class or its prototype. This proposal is also built entirely off of
352+
Symbol-named properties, but doing so using existing mechanisms would be
353+
tedious and difficult to do properly. For an example of the complexity involved
354+
in doing it properly, see the output of the sweet.js implementation.
355+
324356

325-
### ECMAScript proposed bind (`::`) operator
357+
## Links to previous related discussions/strawmen
326358

327359
TODO

0 commit comments

Comments
 (0)