Defining a stored instance for federated data types

How do you define an instance of a persistent vector for a data type, as shown below (composed of primitive GHC types):

data Atoms =  I GHC.Int.Int32|S GHC.Int.Int16 -- define a union data type

I checked this persistent tutorial , but it only works for vectors of the same type, not for combining, as shown above.

+1
source share
1 answer

You need to code which constructor you used to somehow create an instance of the type.

You can, for example, add a byte that indicates the index of the constructor used. This means that the above values ​​can be saved as follows:

Haskell    Binary
I 3     -> 00 00 00 00 03
S 4     -> 01 00 04 XX XX
              ^ Data
           ^ Constructor index
XX = unused byte

, , , , , , ( ) .

+4

All Articles