-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Either instance for IsForeign #32
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Examples.Either where | ||
|
||
import Prelude | ||
import Data.Either | ||
|
||
import Data.Foreign | ||
import Data.Foreign.Class | ||
|
||
import Control.Monad.Eff.Console | ||
|
||
data Point = Point Number Number Number | ||
|
||
instance showPoint :: Show Point where | ||
show (Point x y z) = "Point " ++ show [x, y, z] | ||
|
||
instance pointIsForeign :: IsForeign Point where | ||
read value = Point <$> readProp "x" value | ||
<*> readProp "y" value | ||
<*> readProp "z" value | ||
|
||
type Response = Either (Array String) Point | ||
|
||
main = do | ||
print $ | ||
readJSON """{ "x":1, "y": 2, "z": 3}""" :: F Response | ||
print $ | ||
readJSON """["Invalid parse", "Not a valid y point"]""" :: F Response |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,15 @@ instance arrayIsForeign :: (IsForeign a) => IsForeign (Array a) where | |
readElement :: forall a. (IsForeign a) => Int -> Foreign -> F (Array a) | ||
readElement i value = readWith (ErrorAtIndex i) value | ||
|
||
instance eitherIsForeign :: (IsForeign l, IsForeign r) => IsForeign (Either l r) where | ||
read value = | ||
case (read value) :: F r of | ||
Right rval -> pure (Right rval) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this prefers the Also, you can probably shorten this using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup! My thinking is: Attempt to parse the thing we actually want, and if that fails, then attempt to parse the error case. If you have |
||
Left e -> | ||
case (read value) :: F l of | ||
Right lval -> pure (Left lval) | ||
Left e' -> Left e' | ||
|
||
instance nullIsForeign :: (IsForeign a) => IsForeign (Null a) where | ||
read = readNull read | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this typecheck?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It didn't, and neither
psc-ide
norpulp -w build
compile theexamples
directory so I forgot to change it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, thought I was missing something 😄