Skip to content

Instantly share code, notes, and snippets.

@paulyoung
Last active October 22, 2020 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulyoung/7e51897c612c1ff13a6f1aad7b71e5b4 to your computer and use it in GitHub Desktop.
Save paulyoung/7e51897c612c1ff13a6f1aad7b71e5b4 to your computer and use it in GitHub Desktop.
module Main where
import Prelude
import Control.Monad.Except (runExcept)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Effect.Console (log, logShow)
import Foreign (F)
import Foreign.Generic (defaultOptions, genericDecodeJSON, genericEncodeJSON)
import TryPureScript (render, withConsole)
main = render =<< withConsole do
-- {"contents":1,"tag":"Just"}
log $ genericEncodeJSON defaultOptions $ Just 1
-- Right (Just 1)
logShow $ runExcept (genericDecodeJSON defaultOptions "{\"contents\":1,\"tag\":\"Just\"}" :: F (Maybe Int))
-- The inferred type
--
-- forall t33 t39. Generic (Either String Int) t33 => GenericEncode t33 => GenericEncode t39 => Effect Unit
--
-- has type variables which are not determined by those mentioned in the body of the type:
--
-- t39 could not be determined
--
-- Consider adding a type annotation.
log $ genericEncodeJSON defaultOptions (Left "Something went wrong" :: Either String Int)
log $ genericEncodeJSON defaultOptions (Right 1 :: Either String Int)
-- The inferred type
--
-- forall t46 t53. Generic (Either String Int) t46 => GenericDecode t46 => GenericDecode t53 => Effect Unit
--
-- has type variables which are not determined by those mentioned in the body of the type:
--
-- t53 could not be determined
--
-- Consider adding a type annotation.
logShow $ runExcept (genericDecodeJSON defaultOptions "{\"contents\":1,\"tag\":\"Right\"}" :: F (Either String Int))
logShow $ runExcept (genericDecodeJSON defaultOptions "{\"contents\":\"Something went wrong\",\"tag\":\"Left\"}" :: F (Either String Int))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment