purescript / purescript-record

Functions for working with records and polymorphic labels
BSD 3-Clause "New" or "Revised" License
70 stars 31 forks source link

Rename field? #17

Closed justinwoo closed 6 years ago

justinwoo commented 6 years ago

It seems like a silly thing, but I have a need for this with simple-json usage. Should we add one to this library?

So far my implementation is as follows (from here):

rename :: forall prev next ty input inter output
   . IsSymbol prev
  => IsSymbol next
  => RowCons prev ty inter input
  => RowLacks prev inter
  => RowCons next ty inter output
  => RowLacks next inter
  => SProxy prev
  -> SProxy next
  -> Record input
  -> Record output
rename prev next record =
  insert next value inter
  where
    value = get prev record
    inter :: Record inter
    inter = delete prev record
paf31 commented 6 years ago

This seems useful, but it seems a bit specific and easy enough to write out when needed (insert next (get prev record) (delete prev record) once inlined). I'm thinking a record-extras library might make sense for this sort of thing.

justinwoo commented 6 years ago

I don't think it can be solved inlined though... at least, I cannot get this to compile:

rename :: forall prev next ty input output
   . IsSymbol prev
  => IsSymbol next
  => RowCons prev ty output input
  => RowLacks prev output
  => RowCons next ty input output
  => RowLacks next input
  => SProxy prev
  -> SProxy next
  -> Record input
  -> Record output
rename prev next record =
  insert next (get prev record) (delete prev record)

and it complains with this:

Could not match type

     output7

   with type

     input6

 while solving type class constraint

   Prim.RowCons prev4
                t2
                t5
                input6

 while applying a function delete
   of type IsSymbol t0 => RowLacks t0 t1 => RowCons t0 t2 t1 t3 => SProxy t0 -> { | t3 } -> { | t1 }
   to argument prev
 while inferring the type of delete prev
 in value declaration rename

 where output7 is a rigid type variable
       input6 is a rigid type variable
       prev4 is a rigid type variable
       t5 is an unknown type
       t2 is an unknown type
       t0 is an unknown type
       t3 is an unknown type
       t1 is an unknown type
 (psc-ide)
justinwoo commented 6 years ago

and yeah, this works:

rename :: forall prev next ty input inter output
   . IsSymbol prev
  => IsSymbol next
  => RowCons prev ty inter input
  => RowLacks prev inter
  => RowCons next ty inter output
  => RowLacks next inter
  => SProxy prev
  -> SProxy next
  -> Record input
  -> Record output
rename prev next record =
  insert next (get prev record) (delete prev record :: Record inter)
paf31 commented 6 years ago

Ok, seems useful then. Maybe we should add it to Builder too.