Skip to content

Add prop variant using VTA #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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` (#145 by @amesgen)

Bugfixes:

Expand Down
3 changes: 2 additions & 1 deletion packages.dhall
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let upstream =
https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall
https://raw.githubusercontent.com/purescript/package-sets/psc-0.15.10-20230719/src/packages.dhall
sha256:dfc2383cad9ae1beea830197d36ef39aed9d4cd587c0af04b8fce252209a2f0d

in upstream
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