Skip to content

Instantly share code, notes, and snippets.

@kozross

kozross/Via.hs Secret

Last active June 28, 2021 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kozross/961b0b84cce9d94e31a4ba60cc0109c9 to your computer and use it in GitHub Desktop.
Save kozross/961b0b84cce9d94e31a4ba60cc0109c9 to your computer and use it in GitHub Desktop.
{-# 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