danidiaz / dep-t

Dependency injection for records-of-functions.
http://hackage.haskell.org/package/dep-t
BSD 3-Clause "New" or "Revised" License
8 stars 2 forks source link

Add helper newtype for deriving Has instances using DerivingVia #9

Closed danidiaz closed 3 years ago

danidiaz commented 3 years ago

Has uses DefaultSignatures to provide a default implementation based on HasField and Dep.

It would be nice to support deriving that same instance suing DerivingVia and an auxiliary newtype. That way is more explicit, at the cost of more verbosity.

It would look like this:

newtype TheDefaultFieldName env = TheDefaultFieldName env

instance (Dep r_, HasField (DefaultFieldName r_) (env_ m) u, Coercible u (r_ m)) 
         => Has r_ m (TheDefaultFieldName (env_ m)) where
   dep (TheDefaultFieldName env) = coerce . getField @(DefaultFieldName r_) $ env

And be used like

deriving via (TheDefaultFieldName (EnvHKD I m)) instance Has Controller m (EnvHKD I m)
danidiaz commented 3 years ago

Added in 3a6bb85d34b63ac3d6b4c464f5a808d12eb8317b.