這個問題與another question有關我之前問過。OCaml:解析JSON到循環類型
我正在從JSON文件中讀取數據,並嘗試將它們解析爲我製作的數據類型。對於房間
{
"rooms":
[
{
"id": "room1",
"description": "This is Room 1. There is an exit to the north.\nYou should drop the white hat here.",
"items": ["black hat"],
"points": 10,
"exits": [
{
"direction": "north",
"room": "room2"
}
],
"treasure": ["white hat"]
},
{
"id": "room2",
"description": "This is Room 2. There is an exit to the south.\nYou should drop the black hat here.",
"items": [],
"points": 10,
"exits": [
{
"direction": "south",
"room": "room1"
}
],
"treasure": ["black hat"]
}
]
}
我的用戶定義類型是:
type room = {
room_id : int ;
room_description : string ;
room_items : item list ;
room_points : int ;
room_exits : exit list ;
room_treasure : item list ;
}
and exit = direction * room
然而,房間有一個「退出」字段,它本身就是一個「房間」類型。然後,當我嘗試創建記錄room1時,我首先需要定義room2,但爲了定義room2,我需要知道room1。這看起來像循環類型。
任何人都可以幫助我嗎?