-
-
Save kozross/961b0b84cce9d94e31a4ba60cc0109c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DerivingVia #-} | |
{-# LANGUAGE KindSignatures #-} | |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
module Via where | |
import Data.Kind (Type) | |
import Data.Semigroup (Last) | |
import Data.Functor.Compose (Compose) | |
newtype Foo (a :: Type) = Foo (Maybe (Last a)) | |
-- This can be rewritten as 'deriving newtype (Semigroup, Monoid)' | |
deriving (Semigroup, Monoid) via (Maybe (Last a)) | |
-- This can't | |
deriving (Functor) via (Compose Maybe Last) | |
newtype Bar (a :: Foo) = Foo (Maybe (First a)) | |
deriving (Semigroup, Monoid) via (Maybe (First a)) | |
-- If I were to change Bar's representation away from 'Maybe (First a)', I would get an error from the via derivation. | |
-- This reminds me to check the semantics, make sure it's still doing what I want, etc. | |
-- If I had used a newtype derivation, this would be _silently_ allowed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment