mauke / data-default

A class for types with a default value
45 stars 16 forks source link

Adding the DefaultM monadic interface. #8

Closed wdanilo closed 8 years ago

fizruk commented 9 years ago

Why would you need this specific class? Perhaps an explicit use case might be of help here.

This particular interface can be replaced easily with a newtype:

newtype DefaultM m a = DefaultM { getDefaultM :: m a }

instance (Monad m, Default a) => Default (DefaultM m a) where
  def = DefaultM (return def)

BTW, in presence of AMP we can replace Monad with Applicative and return with pure.

mauke commented 8 years ago

I don't see how this is useful, and overlapping instances scare me.

wdanilo commented 8 years ago

Oh, I think we can discuss it further here. Imagine a record, which has "default" instance, but its default instance can be constructed only when it has access to some monadic stack (it wants for example access GraphBuilder if it is a Node). Then we can just use defaultM here. @mauke why it scares you? I didn't expected such answer to be honest because OverlappingInstances are rather widely used ...

wdanilo commented 8 years ago

In new GHC we can of course use the instance-specific {-# OVERLAPPABLE #-} pragma, which will affect ONLY this instance. Even better - in new GHC we don't have to use any pragma, because we can control it in the files when we use it.

@fizruk: please see my explanation above. I didn't saw your answer then, I'm sorry for that! :) In fact the "default" instance provided in the code could be implemented as default method as well, I mean:

class DefaultM m a where
    -- | The default value for this type in a monad.
    defM :: m a
    default defM :: (Monad m, Default a) => m a
    defM = return def

and in fact it is much better than my original proposition - without any overlapping instances enabled. @mauke would it be ok for you?

mauke commented 8 years ago

I still don't see how this is useful. I haven't run into a problem where I would need it, so it's hard for me to judge whether this would be a good thing.

In the meantime, if you need this, can't you release it as a separate module? AFAICS there's no technical need for it to be in Data.Default itself.

wdanilo commented 8 years ago

@mauke: Of course I can, it just fits perfectly into this module. This is just default instance of a datatype, that can be generated in a monad, that's all. Releasing milions of small modules that provide very similar things for different use cases is a little bit strange, but I can release something like data-default-plus (or something with another name) which will reexport things from data-default with additional classes.