Skip to content

Commit

Permalink
fix: AsType haddocks hide code examples (#165)
Browse files Browse the repository at this point in the history
* fix: haddock syntax error
This was hiding the initial datatypes for the instance examples, which made them very confusing

* feat: add repl examples for other instance methods
  • Loading branch information
Tristano8 authored Dec 24, 2024
1 parent cbe168c commit 23e4872
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
22 changes: 20 additions & 2 deletions generic-lens/src/Data/Generics/Sum/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
module Data.Generics.Sum.Typed
( -- *Prisms
--
-- $setup
-- $setup
AsType (..)
) where

Expand Down Expand Up @@ -61,7 +61,7 @@ import "generic-lens-core" Data.Generics.Internal.Void
-- dog = Dog (MkDog "Shep" (Age 3))
-- cat = Cat "Mog" (Age 5)
-- duck = Duck (Age 2)
-- :}
-- >>> :}


-- |Types that can represent another type, either by being a sum with a
Expand All @@ -87,11 +87,29 @@ class AsType a s where
{-# INLINE _Typed #-}

-- |Inject by type.
--
--
-- >>> :{
-- dog :: Dog
-- dog = MkDog "Fido" (Age 11)
-- dogAsAnimal :: Animal
-- dogAsAnimal = injectTyped dog
-- dogAsItself :: Dog
-- dogAsItself = injectTyped dog
-- >>> :}
injectTyped :: a -> s
injectTyped
= build _Typed

-- |Project by type.
--
--
-- >>> :{
-- dogAsAnimal :: Animal
-- dogAsAnimal = Dog (MkDog "Fido (Age 11)")
-- mDog :: Maybe Dog
-- mDog = projectTyped dogAsAnimal
-- >>> :}
projectTyped :: s -> Maybe a
projectTyped
= either (const Nothing) Just . match _Typed
Expand Down
23 changes: 21 additions & 2 deletions generic-optics/src/Data/Generics/Sum/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
module Data.Generics.Sum.Typed
( -- *Prisms
--
-- $setup
-- $setup
AsType (..)
) where

Expand Down Expand Up @@ -86,12 +86,31 @@ class AsType a s where
_Typed = prism injectTyped (\i -> maybe (Left i) Right (projectTyped i))
{-# INLINE _Typed #-}


-- |Inject by type.
--
--
-- >>> :{
-- dog :: Dog
-- dog = MkDog "Fido" (Age 11)
-- dogAsAnimal :: Animal
-- dogAsAnimal = injectTyped dog
-- dogAsItself :: Dog
-- dogAsItself = injectTyped dog
-- >>> :}
injectTyped :: a -> s
injectTyped
= review _Typed
= build _Typed

-- |Project by type.
--
--
-- >>> :{
-- dogAsAnimal :: Animal
-- dogAsAnimal = Dog (MkDog "Fido (Age 11)")
-- mDog :: Maybe Dog
-- mDog = projectTyped dogAsAnimal
-- >>> :}
projectTyped :: s -> Maybe a
projectTyped
= either (const Nothing) Just . matching _Typed
Expand Down

0 comments on commit 23e4872

Please sign in to comment.