@@ -0,0 +1,47 @@
1+{-# LANGUAGE DataKinds, DeriveGeneric, TypeApplications #-}
5+-- Test case from #24, comments preserved
7+import Data.Generics.Product.Fields
8+import Data.Generics.Product.Positions
11+data Foo a b = Foo { x1 :: a, x2 :: b } deriving (Generic, Show)
13+data Bar a b = Bar { x3 :: Foo a b, x4 :: Int } deriving (Generic, Show)
15+tup :: ((Int, Char), Int)
17+tup2, tup3, tup4 :: ((Char, Char), Int)
18+tup2 = tup & _1 . _1 %~ toEnum -- Works.
19+tup3 = tup & x %~ toEnum -- Works also with type annotation.
20+ where x :: Lens ((Int, Char), Int) ((Char, Char), Int) Int Char
23+tup4 = tup & position @1 . position @1 %~ toEnum
27+foo2, foo3 :: Foo Char Char
28+foo2 = foo & field @"x1" %~ toEnum -- Works when there's just one 'field'.
29+foo3 = foo & position @1 %~ toEnum -- Works when there's just one 'position'.
32+bar = Bar (Foo 1 'a') 2
33+bar2, bar3, bar4 :: Bar Char Char
34+-- Doesn't work, error at first 'field' (Couldn't match type ‘Int’ with ‘Char’ arising from a use of ‘field’).
35+bar2 = bar & field @"x3" . field @"x1" %~ toEnum
36+-- Type annotation doesn't help.
37+bar3 = bar & l %~ toEnum
38+ where l :: Lens (Bar Int Char) (Bar Char Char) Int Char
39+ l = field @"x3" . field @"x1"
40+-- Doesn't work, error at first 'position' (Couldn't match type ‘Int’ with ‘Char’ arising from a use of ‘position’).
41+bar4 = bar & position @1 . position @1 %~ toEnum
42+-- Works if we stick to simple Lens' (modify to the same type).
44+bar5 = bar & field @"x3" . field @"x1" %~ (+1)