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 upjson decoding: undefined is not a function #471
Comments
gpaul
changed the title from
undefined is not a function
to
json decoding: undefined is not a function
Jan 5, 2016
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
gpaul
Jan 5, 2016
The generated code looks like this. Could it be an order of initialization issue, where decodeV references objecter2, which references objecter, which references decodeV again?
var Object = function (a) {
return {ctor: "Object"
,_0: a};
};
var objecter3 = function (d) {
return $Result.Ok(Object(d));
};
var Str = function (a) {
return {ctor: "Str",_0: a};
};
var stringer = function (s) {
return $Result.Ok(Str(s));
};
var stringer2 = A2($Json$Decode.customDecoder,
$Json$Decode.string,
stringer);
var objecter2 = A2($Json$Decode.customDecoder,
objecter,
objecter3);
var objecter = $Json$Decode.dict(decodeV);
var decodeV = $Json$Decode.oneOf(_L.fromArray([objecter2
,stringer2]));
gpaul
commented
Jan 5, 2016
|
The generated code looks like this. Could it be an order of initialization issue, where decodeV references objecter2, which references objecter, which references decodeV again?
|
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jvoigtlaender
Jan 5, 2016
Contributor
See elm/compiler#873 and the various issues occurring as links there that are related to Json.Decode specifically. For example, https://github.com/elm-lang/core/issues/361 and elm/compiler#1244.
So yes, the cause here is the recursive dependencies. And it is well known that it is an issue that should be fixed. If you need a workaround as long as it is not yet fixed, I think the discussion in https://github.com/elm-lang/core/issues/361 (about introducing dummy arguments to delay evaluation) might help you.
|
See elm/compiler#873 and the various issues occurring as links there that are related to So yes, the cause here is the recursive dependencies. And it is well known that it is an issue that should be fixed. If you need a workaround as long as it is not yet fixed, I think the discussion in https://github.com/elm-lang/core/issues/361 (about introducing dummy arguments to delay evaluation) might help you. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
gpaul
commented
Jan 6, 2016
|
Thanks, that works perfectly. Feel free to close this ticket. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
awalterschulze
Jan 6, 2016
So for future reference:
objecter2 becomes
objecter2 : () -> Decoder V
objecter2 () = customDecoder objecter objecter3
And decodeV becomes
decodeV () = oneOf
[
objecter2 ()
, stringer2
]
And main becomes
main =
case (decodeString (decodeV ()) jsonString) of
Ok value -> show value
Err msg -> show msg
Is this correct?
awalterschulze
commented
Jan 6, 2016
|
So for future reference:
And decodeV becomes
And main becomes
Is this correct? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jvoigtlaender
Jan 6, 2016
Contributor
Yes, plus a corresponding change to the use of decodeV in objecter.
|
Yes, plus a corresponding change to the use of |
jvoigtlaender
closed this
Jan 6, 2016
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
awalterschulze
Jan 6, 2016
Ah yes good catch
objecter : Decoder (Dict String V)
objecter = dict (decodeV ())
awalterschulze
commented
Jan 6, 2016
|
Ah yes good catch
|
gpaul commentedJan 5, 2016
When parsing json into an algebraic datatype with Json.Decode.customDecoder and 'oneOf' I get the following error:
"expecting one of the following:\n undefined is not a function\n expecting a String but got {"aaa":"1"}"
The code follows: