Quid2 / flat

Principled and efficient binary serialization
BSD 3-Clause "New" or "Revised" License
60 stars 17 forks source link

'instance Flat Day' reports errors. #2

Closed highfly22 closed 6 years ago

highfly22 commented 7 years ago
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}

import Data.Flat
import Data.Time

instance Flat Day
error:
    • No instance for (Data.Flat.Class.GEncode (Rep Day))
        arising from a use of ‘Data.Flat.Class.$dmencode’
    • In the expression: Data.Flat.Class.$dmencode @Day
      In an equation for ‘encode’:
          encode = Data.Flat.Class.$dmencode @Day
      In the instance declaration for ‘Flat Day’
tittoassini commented 6 years ago

To automatically derive a Flat instance for a data type, the data type must be an instance of Generic.

Unfortunately, this is not the case for Day.

unhammer commented 6 years ago

See https://github.com/fpco/store/blob/247f2863bed0f5c1003ab835075b6dcafb9aa773/src/Data/Store/Internal.hs#L669 for an instance of Day for store – I suppose it'd be something like (completely untested)

size = Data.Flat.Encoder.Size.sInteger
encode = Data.Flat.encode (Time.toModifiedJulianDay d)
decode = Time.ModifiedJulianDay <$> Data.Flat.decode

(it might make sense for flat to also include some instances for the popular non-generic types? or would it be better to have that in a flat-orphans package to avoid deps?)

kk-hainq commented 5 years ago

Is there anything I can help with to make flat-orphans possible? Will anyone support the idea?