Skip to content

Commit d1bc42e

Browse files
authored
Add new and run to Record.ST (#71)
* Add Record.ST.new * Add Record.ST.run * Copy changes to Foreign.Object.run documentation
1 parent 4e864ba commit d1bc42e

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/Record/ST.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
"use strict";
22

3+
exports.run = function (f) {
4+
return f();
5+
};
6+
7+
exports["new"] = function () {
8+
return {};
9+
};
10+
311
function copyRecord(rec) {
412
var copy = {};
513
for (var key in rec) {

src/Record/ST.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Record.ST
22
( STRecord
3+
, run
4+
, new
35
, freeze
46
, thaw
57
, peek
@@ -21,6 +23,15 @@ foreign import data STRecord :: Region -> Row Type -> Type
2123

2224
type role STRecord nominal representational
2325

26+
-- | Freeze a mutable record, creating an immutable record. Use this function as you would use
27+
-- | `Control.Monad.ST.run` (from the `purescript-st` package) to freeze a mutable reference.
28+
-- |
29+
-- | The rank-2 type prevents the record from escaping the scope of `run`.
30+
foreign import run :: forall r. (forall h. ST h (STRecord h r)) -> Record r
31+
32+
-- | Create a new, empty mutable record
33+
foreign import new :: forall h. ST h (STRecord h ())
34+
2435
-- | Freeze a mutable record, creating a copy.
2536
foreign import freeze :: forall h r. STRecord h r -> ST h (Record r)
2637

test/Main.purs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import Prelude
55
import Effect (Effect)
66
import Record (delete, equal, get, insert, merge, modify, rename, set)
77
import Record.Builder as Builder
8-
import Control.Monad.ST (run) as ST
9-
import Record.ST (poke, thaw, freeze, modify) as ST
8+
import Record.ST (run, poke, thaw, modify) as ST
109
import Record.Unsafe (unsafeHas)
1110
import Test.Assert (assert')
1211
import Type.Proxy (Proxy(..))
@@ -45,11 +44,11 @@ main = do
4544
rec <- ST.thaw { x: 41, y: "" }
4645
ST.poke x 42 rec
4746
ST.poke y "testing" rec
48-
ST.freeze rec
47+
pure rec
4948
stTest2 = ST.run do
5049
rec <- ST.thaw { x: 41 }
5150
ST.modify x (_ + 1) rec
52-
ST.freeze rec
51+
pure rec
5352

5453
assert' "pokeSTRecord" $
5554
stTest1.x == 42 && stTest1.y == "testing"

0 commit comments

Comments
 (0)