Keys from record type

#1

Hi, I am very new to purescript, so I hope my question is not too silly.

How can I get the keys from a records type alone? I only found Record.Extra.keys for which I would need an actual record.

Let’s say I have defined my record type like

type MyRecord = { key1 :: String, key2 :: Int }

then I would like to get something like [“key1”, “key2”]

Thank you

#2

It doesn’t need to be a record for you to use Record.Extra.keys, which is indeed confirmed by the source code at https://github.com/justinwoo/purescript-record-extra/blob/v3.0.1/src/Record/Extra.purs#L122


keys :: forall g row rl
   . RL.RowToList row rl
  => Keys rl
  => g row -- this will work for any type with the row as a param!
  -> List String
keys _ = keysImpl (RLProxy :: RLProxy rl)

Thus something like https://pursuit.purescript.org/packages/purescript-prelude/4.1.1/docs/Type.Data.Row#t:RProxy should work.

It may be easier for you to reformulate your type into two parts:


type MyRow = ( key1 :: String, key2 :: Int )
type MyRecord = Record MyRow

myKeys = keys (RProxy :: RProxy MyRow)

There are other ways to do this, but I can’t think of a super simple way to do it right now

2 Likes
#3

Thank you, this works fine.