Skip to content
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

runtime error in Json decoding #704

Closed
goertzenator opened this issue Aug 30, 2016 · 3 comments
Closed

runtime error in Json decoding #704

goertzenator opened this issue Aug 30, 2016 · 3 comments

Comments

@goertzenator
Copy link

I have a Json decoder that creates a JS runtime error when executed. Note that decodeOffsets is factored out as a separate function:

decodeEeprom : Json.Decoder Eeprom
decodeEeprom =
    let
        createEeprom offsets subchannel_config channel_desc =
            { offsets = offsets,
              subchannel_config = subchannel_config,
              channel_desc = channel_desc
            }
        decodeOffsets           = "offsets" := decodeOffsets
        decodeSubchannel_config = "subchannel_config" := Json.list Json.int
        decodeChannel_desc      = "channel_desc" := Json.list Json.string
    in
        Json.object3 createEeprom decodeOffsets decodeSubchannel_config decodeChannel_desc

decodeOffsets = Json.list Json.float

The incorrect JS that is generated is...

var _user$project$Eeprom$decodeEeprom = function () {
    var decodeChannel_desc = A2(
        _elm_lang$core$Json_Decode_ops[':='],
        'channel_desc',
        _elm_lang$core$Json_Decode$list(_elm_lang$core$Json_Decode$string));
    var decodeSubchannel_config = A2(
        _elm_lang$core$Json_Decode_ops[':='],
        'subchannel_config',
        _elm_lang$core$Json_Decode$list(_elm_lang$core$Json_Decode$int));
    var decodeOffsets = A2(_elm_lang$core$Json_Decode_ops[':='], 'offsets', decodeOffsets); //<- decodeOffsets param is undefined!
    var createEeprom = F3(
        function (offsets, subchannel_config, channel_desc) {
            return {offsets: offsets, subchannel_config: subchannel_config, channel_desc: channel_desc};
        });
    return A4(_elm_lang$core$Json_Decode$object3, createEeprom, decodeOffsets, decodeSubchannel_config, decodeChannel_desc);
}();

In the above the decodeOffsets parameter to decodeOffsets = A2(... I assume should actually be referring to this function:

var _user$project$Eeprom$decodeOffsets = _elm_lang$core$Json_Decode$list(_elm_lang$core$Json_Decode$float);

But it doesn't, and it eventually crashes. :(

This is on elm-lang/core-4.0.5.

@process-bot
Copy link

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@goertzenator
Copy link
Author

I've boiled this down to a much simpler example, and it may just be my misunderstanding of the language. In let y = y I assume this to mean "Create a local variable named y with the same value as the module-scoped y." But something else is clearly happening and instead of seeing the output "123" I see "<internal structure>".

I'm leaving the Json case above in place because it manifests in a more serious javascript crash.

import Html exposing (text)

main =
  text (toString x)

x : Int
x =
    let
        --y = 123  -- this outputs "123"
        y = y     -- this outputs "<internal structure>"
    in
        y

y = 123  -- not needed in "y = y" case

@evancz
Copy link
Member

evancz commented Aug 30, 2016

Duplicate of elm/compiler#873

@evancz evancz closed this as completed Aug 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants