Elm 键变量名称中的转换字符串

Elm 键变量名称中的转换字符串,elm,Elm,可以将字符串转换为键,例如: type alias Model = { sun : Int -- Init with 0 , moon : Int -- Init with 0 } 我想要达到的目标: let userSelect = "sun"; in ({ model | userSelect = 1 }, Cmd.none) -- ugly to be easy to understand 在model.sun应该1之后您将无法完全执行您想要的操作,因为访问记录

可以将字符串转换为键,例如:

type alias Model =
  { sun : Int  -- Init with 0
  , moon : Int -- Init with 0
  }
我想要达到的目标:

let
  userSelect = "sun";
in
 ({ model | userSelect = 1 }, Cmd.none)  -- ugly to be easy to understand 

在model.sun应该1之后

您将无法完全执行您想要的操作,因为访问记录的方式不起作用。对你来说,我推荐一本字典

type alias Model =
  { planets : Dict String Int
  }


planets = 
    Dict.fromList 
        [ ("sun", 0)
        , ("moon": 0)
        ]
model = { planets = planets }
然后

let
  userSelect = "sun";
in
 ({ model | planets = Dict.insert userSelect 1 model.planets }, Cmd.none)

这可能有助于更详细地了解您试图用您的模型实现的目标-您是在项目之间进行选择,还是执行多项选择或其他不同的操作?您是否能够取得进展?我生病了,无法再次进入。由于我的项目已经很庞大,它没有时间重修结构。然而,他的反应很好。谢谢。希望你感觉好些