榆树中的通用类型字典

Mat*_*ter 6 f# elm

我很好奇如何从F#移植到Elm:

type World =
    { Rooms: Map<RoomId, Room> 
      Player: Player }
Run Code Online (Sandbox Code Playgroud)

RoomId,Room之间的东西叫做泛型字典.请看这里的上下文:https://github.com/ShalokShalom/Elchemist/blob/master/Game.fs

我读了一些关于类型变量的东西,它们能帮忙吗?如果是这样,怎么样?

感谢:D

Cha*_*ert 7

榆树的语法类似.

编辑 - @dogbert是关于RoomId在我的原始答案中无法比较的.您可以使用String的类型别名.

type alias RoomId = String

type alias Room =
    { id: RoomId
    , details: Details
    , items: List Item
    , exits: Exits 
    }

type alias World =
    { rooms: Dict RoomId Room
    , player: Player
    }
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下你不能对Dict做任何事情,因为`RoomId`不是'可比的'. (3认同)