I recently came across this blog post by Brian McKenna explaining row polymorphism. This seemed like a wonderful idea to me, but then I realized it smells an awful lot like bounded parametric polymorphism:
With row-polymorphism:
sum: {x: int, y: int | rho} -> int
function sum r = r.x + r.y
With bounded parametric polymorphism:
sum: forall a <: {x: int, y: int}. a -> int
function sum r = r.x + r.y
Can someone clarify the differences to polymorphism between these two approaches?
let f x = x with {sum: x.a + x.b}
with both? If I understand correctly, row-polymorphism will allow you to keep whatever extra fields are in thex
but bounded parametric polymorphism will not because you'd want to say that is has typeforall a <: {x: int, y: int}. a -> a_with_a_new_field_sum
and I don't think you can expressa_with_a_new_field_sum
. $\endgroup$with
construct. Since that wasn't laid out in the article, I tried to avoid it, because it confuses matters. $\endgroup$forget
operator that takes a record and removes one of its fields. Then, to typefun r -> forget x of r
, you'd probably also need row polymorphism. $\endgroup$