the typeclass model offers superior extensibility
Problem
Subclassing inheritance is an anti-pattern (see also this and this and this). For those who aren't familiar with nominal typeclasses in for example Rust and Haskell, they conceptually (without getting into details and caveats yet) are a way of adding implementation to existing nominal types without modifying the source for those existing nominal types. For example, if you have an instance of existing nominal type `A` and your want to invoke a function that expects a nominal type `B` input, typeclasses conceptually enable you to declare the implementation of type `B` for type `A` and invoke the said function with the said instance of type `A` as input. The compiler is smart enough to automatically provide (the properties dictionary data structure or in ECMAScript the prototype chain) to the function the type `B` properties on the type `A` instance. The has efficiency, SPOT, and DNRY advantages over the alternative of manually wrapping the instance of `A` in a new instance which has `B` type and delegate to the properties of `A`. Scala has `implicit` conversion to automate, but this doesn't eliminate all the boilerplate and solve the efficiency (<sup><sub>and tangentially note Scala also can implement a typeclass design pattern employing implicits</sub></sup>). This disadvantage of wrapping with new instances compared to typeclasses is especially significant when the instance is a collection (or even collection of collections) of instances (potentially heterogeneous with a un
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: the typeclass model offers superior extensibility
@gcnew We are well aware of the concept of Type Classes, we are just confused as to why editing the prototype chain would in any way facilitate writing them in Javascript. Javascript, unlike Self seems to have advocated embedding over changing delegation from the very start and modern engines like V8 have only pushed this further by focusing optimizations on embedding. If the new setPrototypeOf ge
Trust Score
3 verifications
- 1
@gcnew We are well aware of the concept of Type Classes, we are just confused as
@gcnew We are well aware of the concept of Type Classes, we are just confused as to why editing the prototype chain would in any way facilitate writing them in Javascript. Javascript, unlike Self seems to have advocated embedding over changing delegation from the very start and modern engines like V8 have only pushed this further by focusing optimizations on embedding. If the new setPrototypeOf gets significant use, I assume this will change.
- 2
I'm not against this suggestion, as I don't quite understand what's being sugges
I'm not against this suggestion, as I don't quite understand what's being suggested. The theory is not the problem, I understand how type classes are useful, especially for higher kinded polymorphism. I would just like to see some examples of real Javascript code that can't be typed today and how editing the prototype chain would make this possible.
Validation
Resolved in microsoft/TypeScript GitHub issue #10844. Community reactions: 4 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep