Skip to content

Generic for newtype not found when in a Sum type #59

Open
@tonicebrian

Description

@tonicebrian

I have these data structures:

import Data.Generic.Rep (class Generic)

opts :: Options
opts = defaultOptions { unwrapSingleConstructors = true }

data Sum
  = Nullary
  | Unary Int
  | UnaryNewType WrappedInt

newtype WrappedInt
  = WrappedInt Int

derive instance sumGeneric :: Generic Sum _

derive instance wrappedGeneric :: Generic WrappedInt _

If I do

genericEncodeJSON opts (WrappedInt 8)

I corretcly get a 8 but If I do

genericEncodeJSON opts (UnaryNewType (WrappedInt 8))

I get an error about not being able to find the generic instance that was used in the first example:

  No type class instance was found for                                                                                                      
                                           
    Foreign.Generic.Class.Encode WrappedInt
                                           

while applying a function genericEncodeJSON
  of type Generic t0 t1 => GenericEncode t1 => { fieldTransform :: String -> String 
                                               , sumEncoding :: SumEncoding         
                                               , unwrapSingleArguments :: Boolean   
                                               , unwrapSingleConstructors :: Boolean
                                               }                                    
                                               -> t0 -> String                      
  to argument opts
while inferring the type of genericEncodeJSON opts
in value declaration main

What am I doing wrong here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions