Reputation: 1965
Consider these datatypes:
data SomeNestedData = SomeNestedData { sndValue :: Int }
data OtherNestedData = OtherNestedData { ondValue :: Int }
data Foo = Bar SomeNestedData | Baz OtherNestedData
How about constructing a lens Lens' Foo Int
that would get and set a value of type Int
regardless of the value of some foo :: Foo
?
There is choosing
and assuming canonical lenses (and prisms) for the above datatypes we can do:
lensFooInt :: Lens' (Either Foo Foo) Int
lensFooInt = (_Bar . _sndValue ) `choosing` (_Baz . ondValue)
But look at the type of lensFooInt
. That's not what I expected.
Leading to two questions:
lensFooInt
in a meaningful way? The Either
kind of rules out that it works like I thought.lensFooInt' :: Lens' Foo Int
?Edit: I can actually do this:
lensFooInt' = re _Right . ((_Bar . _sndValue ) `choosing` (_Baz . ondValue))
Upvotes: 3
Views: 129