您可以在 elm 中使用字符串进行记录访问吗?

tla*_*uke 2 elm

如果我有一个看起来像记录中字段名称的字符串,我可以使用它以某种方式获取数据吗?就像是 :

."name".toKey bill 
bill.(asSymbol "name")
Run Code Online (Sandbox Code Playgroud)

——

song =
  { title = "foot", artist = "barf", number = "13" }

fieldsILike : List String
fieldsILike =
  [ "title", "artist" ]


val song key =
  .key song

foo = List.map (val song) fieldsILike --> should be ["foot", "barf"]
Run Code Online (Sandbox Code Playgroud)

And*_*rle 5

不,但你可以使用字典

import Dict exposing (get ,fromList)

song = fromList [ ("title", "foot"), ("artist", "barf") ]

get "title" song -- Just "foot"
get "artist" song -- Just "barf"
get "test" song  -- Nothing
Run Code Online (Sandbox Code Playgroud)