purescript / purescript-record

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

Is the ST module at all useful? #76

Closed garyb closed 3 years ago

garyb commented 3 years ago

This may be a case of serious user-error problem here, but it seems to me that Record.ST is only useful in a very specific and probably not common situation: you can only use it to modify the values of existing fields in a record.

This isn't a complaint about thaw being the only way to get an STRecord, I see that there's an unreleased addition of a new - but actually this is kinda useless, since you can't do anything with an STRecord h () aside from freeze or run. You cannot use the ST interface to add or remove fields.

Given that it only allows modification, at the expense of one copy (at a minimum, two if freeze is used rather than run), it seems like this module may as well not exist - you can just modify an object with update syntax or by rebuilding it completely for the same cost.

I don't think it's fixable either - I think it could be done as an indexed monad perhaps, tracking the row in/out of each operation as the indexed part, but then it wouldn't be ST anymore either.

If I'm wrong here, apologies, can someone show me how you'd use this to build a record from scratch. :smile:

natefaubion commented 3 years ago

I have never had a use for it. However I could see using it with a Global region to have a record with mutable fields.

JordanMartinez commented 3 years ago

then it wouldn't be ST anymore either.

We could just make ST an indexed monad.

kritzcreek commented 3 years ago

If the only good way to use this is with a Global region it should just work in Effect instead, I know I would've had a use for this a couple of times recently where handling a record full of Refs was a little annoying.

kl0tl commented 3 years ago

Ouch, that’s embarrassing, I didn’t even notice that there’s no way to add fields to a mutable record with the ST interface 🙈 I opened https://github.com/purescript/purescript-record/pull/77 to remove Record.ST.run.

hdgarrood commented 3 years ago

I don’t think we should make ST an indexed monad just to satisfy one use case which nobody seems interested in anyway. I think the use case of wanting to have a record where all the fields are mutable and performance is too much of a concern to just put the whole record in the Ref is probably too niche to deserve a spot in core, so my preference would be removing the whole module.