Skip to content

Commit

Permalink
Add prop variant using VTA
Browse files Browse the repository at this point in the history
  • Loading branch information
amesgen committed Jul 19, 2023
1 parent c3d48cf commit d0032d5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Added a variant of `prop` in `Data.Lens.Record.VTA` that supplies the `Symbol` via VTA (visible type application) instead of via `Proxy`.

Bugfixes:

Expand Down
27 changes: 27 additions & 0 deletions src/Data/Lens/Record/VTA.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Data.Lens.Record.VTA (prop) where

import Data.Lens (Lens)
import Data.Symbol (class IsSymbol)
import Prim.Row as Row
import Type.Proxy (Proxy(..))
import Data.Lens.Record as Data.Lens.Record

-- | Construct a (type-changing) lens for a record property, by providing a
-- | `Symbol` via VTA (visible type application) which corresponds to the
-- | property label.
-- |
-- | The lens is polymorphic in the rest of the row of property labels.
-- |
-- | For example:
-- |
-- | ```purescript
-- | prop @"foo"
-- | :: forall a b r. Lens { foo :: a | r } { foo :: b | r } a b
-- | ```
prop
:: forall @l r1 r2 r a b
. IsSymbol l
=> Row.Cons l a r r1
=> Row.Cons l b r r2
=> Lens (Record r1) (Record r2) a b
prop = Data.Lens.Record.prop (Proxy @l)
7 changes: 7 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Data.Lens.Index (ix)
import Data.Lens.Indexed (itraversed, reindexed)
import Data.Lens.Lens (IndexedLens, cloneIndexedLens, ilens)
import Data.Lens.Record (prop)
import Data.Lens.Record.VTA as VTA
import Data.Lens.Setter (iover)
import Data.Lens.Traversal (cloneTraversal)
import Data.Lens.Zoom (ATraversal', IndexedTraversal', Lens, Lens', Traversal, Traversal', zoom)
Expand All @@ -28,6 +29,12 @@ foo = prop (Proxy :: Proxy "foo")
bar :: forall a b r. Lens { bar :: a | r } { bar :: b | r } a b
bar = prop (Proxy :: Proxy "bar")

foo' :: forall a b r. Lens { foo :: a | r } { foo :: b | r } a b
foo' = VTA.prop @"foo"

bar' :: forall a b r. Lens { bar :: a | r } { bar :: b | r } a b
bar' = VTA.prop @"bar"

barAndFoo :: forall a b r. Getter' { bar :: a, foo :: b | r } (Tuple a b)
barAndFoo = takeBoth bar foo

Expand Down

0 comments on commit d0032d5

Please sign in to comment.