假设两种数据类型:Elm是否允许循环引用?
type alias Player =
{ name : String
, team : Team
}
type alias Team =
{ name : String
, players : List Player
}
这JSON:
{
"players": [
{ "id": 100, "name": "Sam Bradford", "teamId": 200 },
{ "id": 101, "name": "Kyle Rudolph", "teamId": 200 },
{ "id": 102, "name": "Matthew Stafford", "teamId": 201 },
{ "id": 103, "name": "Marvin Jones Jr.", "teamId": 201 },
{ "id": 104, "name": "Golden Tate", "teamId": 201 },
],
"teams": [
{ "id": 200, "name": "Minnesota Vikings" },
{ "id": 201, "name": "Detroit Lions" },
]
}
很明显,这个JSON可以被解码成非空链接的对象,而这可以通过一个JSON解码器确定它正在解码数据。有没有办法解码这个JSON并创建链接的数据结构?我不确定如何用纯粹不可变的数据结构来做到这一点,或者如果可能的话。
顺便说一句:处理带有'Dict'和'ID'的循环引用也是@ChadGilbert在他的回答中提到的。 – wintvelt