File tree Expand file tree Collapse file tree 2 files changed +41
-4
lines changed
generic-lens/src/Data/Generics/Sum
generic-optics/src/Data/Generics/Sum Expand file tree Collapse file tree 2 files changed +41
-4
lines changed Original file line number Diff line number Diff line change 27
27
module Data.Generics.Sum.Typed
28
28
( -- *Prisms
29
29
--
30
- -- $setup
30
+ -- $setup
31
31
AsType (.. )
32
32
) where
33
33
@@ -61,7 +61,7 @@ import "generic-lens-core" Data.Generics.Internal.Void
61
61
-- dog = Dog (MkDog "Shep" (Age 3))
62
62
-- cat = Cat "Mog" (Age 5)
63
63
-- duck = Duck (Age 2)
64
- -- :}
64
+ -- >>> :}
65
65
66
66
67
67
-- | Types that can represent another type, either by being a sum with a
@@ -87,11 +87,29 @@ class AsType a s where
87
87
{-# INLINE _Typed #-}
88
88
89
89
-- | Inject by type.
90
+ --
91
+ --
92
+ -- >>> :{
93
+ -- dog :: Dog
94
+ -- dog = MkDog "Fido" (Age 11)
95
+ -- dogAsAnimal :: Animal
96
+ -- dogAsAnimal = injectTyped dog
97
+ -- dogAsItself :: Dog
98
+ -- dogAsItself = injectTyped dog
99
+ -- >>> :}
90
100
injectTyped :: a -> s
91
101
injectTyped
92
102
= build _Typed
93
103
94
104
-- | Project by type.
105
+ --
106
+ --
107
+ -- >>> :{
108
+ -- dogAsAnimal :: Animal
109
+ -- dogAsAnimal = Dog (MkDog "Fido (Age 11)")
110
+ -- mDog :: Maybe Dog
111
+ -- mDog = projectTyped dogAsAnimal
112
+ -- >>> :}
95
113
projectTyped :: s -> Maybe a
96
114
projectTyped
97
115
= either (const Nothing ) Just . match _Typed
Original file line number Diff line number Diff line change 27
27
module Data.Generics.Sum.Typed
28
28
( -- *Prisms
29
29
--
30
- -- $setup
30
+ -- $setup
31
31
AsType (.. )
32
32
) where
33
33
@@ -86,12 +86,31 @@ class AsType a s where
86
86
_Typed = prism injectTyped (\ i -> maybe (Left i) Right (projectTyped i))
87
87
{-# INLINE _Typed #-}
88
88
89
+
89
90
-- | Inject by type.
91
+ --
92
+ --
93
+ -- >>> :{
94
+ -- dog :: Dog
95
+ -- dog = MkDog "Fido" (Age 11)
96
+ -- dogAsAnimal :: Animal
97
+ -- dogAsAnimal = injectTyped dog
98
+ -- dogAsItself :: Dog
99
+ -- dogAsItself = injectTyped dog
100
+ -- >>> :}
90
101
injectTyped :: a -> s
91
102
injectTyped
92
- = review _Typed
103
+ = build _Typed
93
104
94
105
-- | Project by type.
106
+ --
107
+ --
108
+ -- >>> :{
109
+ -- dogAsAnimal :: Animal
110
+ -- dogAsAnimal = Dog (MkDog "Fido (Age 11)")
111
+ -- mDog :: Maybe Dog
112
+ -- mDog = projectTyped dogAsAnimal
113
+ -- >>> :}
95
114
projectTyped :: s -> Maybe a
96
115
projectTyped
97
116
= either (const Nothing ) Just . matching _Typed
You can’t perform that action at this time.
0 commit comments