I was trying to simplify my life with newtype with coerce, but I've encountered a quite painful problem when using it in certain scenario:
import Data.Coerce (coerce)
import Data.Foldable (toList)
-- | newtype instance wrapping foldable type (not necessary [Int])
newtype Foo = Foo [Int]
bar :: [Int]
bar = toList $ coerce $ Foo [1,2,3]
This fails as Haskell is not able to deduce the type correctly.
Is there a way to force this to be correctly resolved? (without directly specifying what coerce should coerce into) Or maybe there is other clean solution? (SO answers suggest mostly the coerce)
The
lenssolution you may be looking for is inControl.Lens.Wrapped: