logoalt Hacker News

derriztoday at 5:27 AM3 repliesview on HN

HM handles sub-typing just fine? Numerous approaches have been known since the 1980s - Michael Wand’s row polymorphism is one such approach.

https://en.wikipedia.org/wiki/Row_polymorphism


Replies

whizztertoday at 8:42 AM

Structural subtyping yes, nominal subtyping is a bit pricklier.

As a developer I personally prefer structural subtyping, but structural subtyping is harder for a compiler to optimize runtime performance for.

Nominal sub-type hierarchies allows for members to be laid out linearly and member accesses becomes just an offset whereas a structural system always has the "diamond problem" to solve (it's hidden from users so not a "problem" but will still haunt compiler/runtime developers).

Now the kicker though, in practice nominal subtype polymorphism has other issues for performance on _modern_ computers since they create variable sized objects and cannot be packed linearly like monomorphic structures.

In the 90s when languages settled on nominal typing memory speeds weren't really an huge issue, but today we know that we should rather compose data to achieve data-polymorphic effects and singular types should be directed to packing.

Thus, most performance benefits of a nominal type system over a structural don't help much in real-life code and maintenance wise we would probably have been better off using structural types (iirc Go went there and interfaces in Java/C# achieves mostly the same effect in practice).

epolanskitoday at 8:35 AM

I've been implementing row polymorphism in my fork of Elm in order to support proper sum and substraction operations on unions, and it's far from trivial.

Example usecase: an Effect may fail with 2 types, but actually you have handled one/catched one so you want to remove it.

Elm-like HM systems handle fine, as you say it, row polymorphism mostly over records.

I'm not an expert in all of this, started studying this recently, so take my words with a grain of salt.

tonygtoday at 8:27 AM

*Mitchell Wand