Skip to content

Instantly share code, notes, and snippets.

@zyla
Created May 11, 2018 17:05
Show Gist options
  • Save zyla/888ad96926decc8ec0fda0cb586b9361 to your computer and use it in GitHub Desktop.
Save zyla/888ad96926decc8ec0fda0cb586b9361 to your computer and use it in GitHub Desktop.
module Main where
import Prelude
import Data.Maybe
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Argonaut (stringify)
import Data.Argonaut.Generic.Aeson as GenericAeson
import Data.Argonaut.Generic.Argonaut as GenericArgonaut
import Data.Foreign.Generic (defaultOptions, encodeJSON, genericEncode)
import Data.Foreign.Generic.Class (class GenericEncode)
import Data.Generic as OldGeneric
import Data.Generic.Rep (class Generic)
data Foo =
C1 { foo :: Int, bar :: String }
| C2
derive instance genericFoo :: OldGeneric.Generic Foo
derive instance genericRepFoo :: Generic Foo _
testValue :: Foo
testValue = C1 { foo: 5, bar: "bar" }
encodeArgonaut = stringify <<< GenericArgonaut.encodeJson
encodeArgonautAeson = stringify <<< GenericAeson.encodeJson
-- encodeForeign :: forall t110 t111. Generic t111 t110 => GenericEncode t110 => t111 -> String
encodeForeign = encodeJSON <<< genericEncode defaultOptions
newtype Bar = Bar { foo :: Maybe Int }
derive instance genericBar :: OldGeneric.Generic Bar
derive instance genericRepBar :: Generic Bar _
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
log "argonaut-generic-codecs aeson"
log $ encodeArgonautAeson testValue
log "foreign-generic"
log $ encodeForeign testValue
log "argonaut-generic-codecs argonaut"
log $ encodeForeign testValue
let testValue2 = Bar { foo: Nothing }
log "argonaut-generic-codecs aeson"
log $ encodeArgonautAeson testValue2
log "foreign-generic"
log $ encodeForeign testValue2
log "argonaut-generic-codecs argonaut"
log $ encodeForeign testValue2
{-
Error found:
in module Main
at src/Main.purs line 31, column 1 - line 31, column 60
The inferred type
forall t110 t111. Generic t111 t110 => GenericEncode t110 => t111 -> String
has type variables which are not mentioned in the body of the type. Consider adding a type annotation.
in value declaration encodeForeign
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment