Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending existing Record type

Tags:

purescript

If I have a record type:

type Rec_ab = { a :: String, b :: Int }

Can I get from it type which is effectfully extended with {c :: Boolean} : { a :: String, b :: Int, c :: Boolean }?

If I had a row, I could do it:

Row_ab = ( a :: String, b :: Int )

Rec_abc = { c :: Boolean | Row_ab }

but if I have a record Rec_ab how this should be solved? I'm not aware of the way of going from Record -> Row.

like image 752
WHITECOLOR Avatar asked Oct 21 '25 03:10

WHITECOLOR


1 Answers

In general, you can't.

If you need that kind of extensibility, your approach with Row_ab is good enough, but another possible solution is to parametrize the record and then use the parameter to extend it:

type Rec_ab r = { a :: String, b :: Int | r }
type Rec_abc = Rec_ab ( c :: Boolean )

This also allows you to write functions that would work with both Rec_ab and Rec_abc:

add42 :: forall r. Rec_ab r -> Rec_ab r
add42 r = r { b = r.b + 42 }
like image 170
Fyodor Soikin Avatar answered Oct 25 '25 06:10

Fyodor Soikin