Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upGenerated JS blows up at runtime due to vars placed in the wrong order #305
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
JoshCheek
Jul 22, 2015
Also. Not sure if this is expected behaviour or not, but for a couple hours I couldn't get any decent feedback on why it was blowing up until I took out the HTTP request and mailbox.
If you compile this code in the same environment as the one above:
import Http
import Json.Decode exposing (..)
import Graphics.Element
import Task exposing (Task)
main = Signal.map Graphics.Element.show receiveParsed.signal
receiveParsed = Signal.mailbox initialValue
initialValue = Ok -1
port parseJson : Task Http.Error ()
port parseJson =
Http.post decodeByKind "http://localhost:3004" (Http.string "1.a")
`Task.andThen` report
report parsed = Signal.send receiveParsed.address (Ok parsed)
decodeByKind = "kind" := string `andThen` chooseDecoder
chooseDecoder k = if k == "child" then decodeChild else decodeParent
decodeChild = "value" := int
decodeParent = "value" := decodeByKindAnd then run it against this server, the initial value will stay at Ok -1 and nothing will give any indication that it even attempted to parse it.
echo -n $'HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 52\r\nAccess-Control-Allow-Origin: null\r\n\r\n{"kind":"parent","value":{"kind":"child","value":1}}' | nc -l 3004If you then consolidate chooseDecoder into decodeByKind, and run the server again, it will correctly parse 1
decodeByKind = "kind" := string
`andThen`
\k -> if k == "child" then decodeChild else decodeParent
JoshCheek
commented
Jul 22, 2015
|
Also. Not sure if this is expected behaviour or not, but for a couple hours I couldn't get any decent feedback on why it was blowing up until I took out the HTTP request and mailbox. If you compile this code in the same environment as the one above: import Http
import Json.Decode exposing (..)
import Graphics.Element
import Task exposing (Task)
main = Signal.map Graphics.Element.show receiveParsed.signal
receiveParsed = Signal.mailbox initialValue
initialValue = Ok -1
port parseJson : Task Http.Error ()
port parseJson =
Http.post decodeByKind "http://localhost:3004" (Http.string "1.a")
`Task.andThen` report
report parsed = Signal.send receiveParsed.address (Ok parsed)
decodeByKind = "kind" := string `andThen` chooseDecoder
chooseDecoder k = if k == "child" then decodeChild else decodeParent
decodeChild = "value" := int
decodeParent = "value" := decodeByKindAnd then run it against this server, the initial value will stay at echo -n $'HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 52\r\nAccess-Control-Allow-Origin: null\r\n\r\n{"kind":"parent","value":{"kind":"child","value":1}}' | nc -l 3004If you then consolidate decodeByKind = "kind" := string
`andThen`
\k -> if k == "child" then decodeChild else decodeParent |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Jul 22, 2015
Member
Is this a version of elm/compiler#873? Are all the pieces of the original example needed to replicate the error?
|
Is this a version of elm/compiler#873? Are all the pieces of the original example needed to replicate the error? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
JoshCheek
Jul 22, 2015
Is this a version of elm/compiler#873?
I don't think so. That sounds like it's about lazy data structures. I very intentionally removed as much complexity as I could, so the data structure is only an Int now. It also works if you tweak it any of about 10 different ways.
If I had to guess, I'd assume there's some path that expected a function to be wrapped around the call so that the order of the variable definitions didn't matter. Other thing that seems potentially likely would be something to do with the native implementations of the JSON lib. Or possibly some edge case in how it resolves which functions depend on which ones.
Are all the pieces of the original example needed to replicate the error?
Yeah, you can paste that first example into http://elm-lang.org/try and see the same result. eg
JoshCheek
commented
Jul 22, 2015
I don't think so. That sounds like it's about lazy data structures. I very intentionally removed as much complexity as I could, so the data structure is only an Int now. It also works if you tweak it any of about 10 different ways. If I had to guess, I'd assume there's some path that expected a function to be wrapped around the call so that the order of the variable definitions didn't matter. Other thing that seems potentially likely would be something to do with the native implementations of the JSON lib. Or possibly some edge case in how it resolves which functions depend on which ones.
Yeah, you can paste that first example into http://elm-lang.org/try and see the same result. eg |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Sep 22, 2016
Member
Okay, so this is valid code, not like elm/compiler#873, but it points out an issue in code generation. I'm going to add a note to elm/compiler#873 about this and try to tackle them both at the same time.
Thanks for the report!
|
Okay, so this is valid code, not like elm/compiler#873, but it points out an issue in code generation. I'm going to add a note to elm/compiler#873 about this and try to tackle them both at the same time. Thanks for the report! |

JoshCheek commentedJul 22, 2015
Using Elm 0.15.1
This example blows up at runtime:
When compiling with
elm make --output BrokenJson.html BrokenJson.elmThis generates the following js at line 3205 (reformatted slightly to be easier to read). When the two variables are swapped around, it then works correctly.Not sure if it's relevant, but my
elm-stuff/exact-dependencies.jsonis:{ "evancz/virtual-dom": "2.0.0", "evancz/elm-http": "1.0.0", "evancz/start-app": "1.0.1", "evancz/elm-html": "4.0.1", "elm-lang/core": "2.1.0" }