Drop Record Fields

I am trying to convert { ... | r} -> Record r is there a function that removes record fields based on the resultant type?

1 Like

I have looked in purescript-record and the only thing that may be correct is nub but the description of “A coercion which removes duplicate labels from a record’s type” doesn’t seem to be quite right.

Seems like delete is pretty close if you know the concrete record fields you want removed. I think you can even build those SProxys dynamically at runtime, though I imagine proving the type constraints needed in the delete function would be pretty difficult.

Is it the type you’re trying to do, or the implementation?

For the type, you’d do Cons "fieldToRemove" _1 r1 r2 => Record r2 -> Record r1, using Cons “backwards” basically.

If I have a record with the set of keys {a :: Int, b :: Int | r} and a function forall b. Record r -> b (r type var shared), can I just pass {a :: Int, b :: Int | r} to that function? I do not want the function to accept extra keys because I only want it to encode to json the fields in r.

I’m pretty confident you can

yourFunction 
$ delete (SProxy :: SProxy "a") 
$ delete (SProxy :: SProxy "b") yourRecord

though I say that without trying to compile it myself…

delete gets to be quite annoying because I have a lot of fields.
This seems like something that purescript-records should have since they already have union.
I think I’ll start an issue there.

Have you tried using Union backwards like the Cons method of removing fields?

1 Like

@garyb Going through the issues your answer makes sense now. I will try Union