faylang / fay

A proper subset of Haskell that compiles to JavaScript
https://github.com/faylang/fay/wiki
BSD 3-Clause "New" or "Revised" License
1.29k stars 86 forks source link

Runtime error involving JSON parsing and tuples #391

Closed relrod closed 10 years ago

relrod commented 10 years ago

I'm running into an issue when I attempt to parse a JSON field into a 2-tuple, then access the elements of the tuple.

I've included a simplified reproducer which demonstrates the issue. It compiles perfectly fine with Fay, but throws a runtime error. :(

I'm unsure if this is a bug or if I am just doing something wrong and not getting a good error. :/

module Main where
import FFI
import Prelude

data Foo = Foo {
    someString :: String
  , twoDoubles :: (Double, Double)
  }

parseFoo :: String -> Fay Foo
parseFoo = ffi "JSON.parse(%1)"

alert :: String -> Fay ()
alert = ffi "alert(%1)"

main :: Fay ()
main = do
  let myFoo = "{\"someString\": \"hello there\", \"twoDoubles\": [12.345, 98.765]}"
  parsed <- parseFoo myFoo
  alert (show $ twoDoubles parsed)  -- [12.345, 98.765] as expected
  alert (show $ fst (twoDoubles parsed)) -- throws error, instead of giving 12.345
                                         -- Uncaught TypeError: Cannot read property 'cdr' of undefined
bergmark commented 10 years ago

The issues was that the instance field is missing in the JSON, #392

I also added some info about the format to the wiki: https://github.com/faylang/fay/wiki/Foreign-function-interface#wiki-serialization-format