Skip to content

Commit c5324ce

Browse files
committed
Add prop variant using VTA
1 parent c3d48cf commit c5324ce

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Added a variant of `prop` in `Data.Lens.Record.VTA` that supplies the `Symbol` via VTA (visible type application) instead of via `Proxy` (#145 by @amesgen)
1011

1112
Bugfixes:
1213

src/Data/Lens/Record/VTA.purs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Data.Lens.Record.VTA (prop) where
2+
3+
import Data.Lens (Lens)
4+
import Data.Symbol (class IsSymbol)
5+
import Prim.Row as Row
6+
import Type.Proxy (Proxy(..))
7+
import Data.Lens.Record as Data.Lens.Record
8+
9+
-- | Construct a (type-changing) lens for a record property, by providing a
10+
-- | `Symbol` via VTA (visible type application) which corresponds to the
11+
-- | property label.
12+
-- |
13+
-- | The lens is polymorphic in the rest of the row of property labels.
14+
-- |
15+
-- | For example:
16+
-- |
17+
-- | ```purescript
18+
-- | prop @"foo"
19+
-- | :: forall a b r. Lens { foo :: a | r } { foo :: b | r } a b
20+
-- | ```
21+
prop
22+
:: forall @l r1 r2 r a b
23+
. IsSymbol l
24+
=> Row.Cons l a r r1
25+
=> Row.Cons l b r r2
26+
=> Lens (Record r1) (Record r2) a b
27+
prop = Data.Lens.Record.prop (Proxy @l)

test/Main.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Data.Lens.Index (ix)
1212
import Data.Lens.Indexed (itraversed, reindexed)
1313
import Data.Lens.Lens (IndexedLens, cloneIndexedLens, ilens)
1414
import Data.Lens.Record (prop)
15+
import Data.Lens.Record.VTA as VTA
1516
import Data.Lens.Setter (iover)
1617
import Data.Lens.Traversal (cloneTraversal)
1718
import Data.Lens.Zoom (ATraversal', IndexedTraversal', Lens, Lens', Traversal, Traversal', zoom)
@@ -28,6 +29,12 @@ foo = prop (Proxy :: Proxy "foo")
2829
bar :: forall a b r. Lens { bar :: a | r } { bar :: b | r } a b
2930
bar = prop (Proxy :: Proxy "bar")
3031

32+
foo' :: forall a b r. Lens { foo :: a | r } { foo :: b | r } a b
33+
foo' = VTA.prop @"foo"
34+
35+
bar' :: forall a b r. Lens { bar :: a | r } { bar :: b | r } a b
36+
bar' = VTA.prop @"bar"
37+
3138
barAndFoo :: forall a b r. Getter' { bar :: a, foo :: b | r } (Tuple a b)
3239
barAndFoo = takeBoth bar foo
3340

0 commit comments

Comments
 (0)