What is this `deriving newtype` syntax?
Asked Answered
E

1

6

From a blog post I read

-- | Newtype for disabling logging
newtype NoLoggingT m a
  = NoLoggingT { runNoLoggingT :: m a }
  deriving newtype (Functor, Applicative, Monad)
  deriving (MonadTrans) via IdentityT

instance Monad m => MonadLog (NoLoggingT m) where logLn _ _ = pure ()

What is thas deriving newtype syntax? Which extension is it and what does it do? Please provide a link to its documentation in the anwser.

Earplug answered 12/8, 2020 at 6:39 Comment(1)
It is helpful to understand DerivingVia as a "Generalized GeneralizedNewtypeDeriving": deriving newtype (F, A, M) can be replaced with deriving (F, A, M) via m. I do not recommend it in practice (always use deriving newtype when you are deriving via the underlying representation of a newtype) but nevertheless good to understand it as a special case of another feature.Loralorain
R
8

It lets GHC use GeneralizedNewtypeDeriving strategy to derive instances. You need to enable DerivingStrategies extension.

Resource answered 12/8, 2020 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.