I am just curious, will this kind of syntax be supported later?
let personName = person["name"]
let nameKey = "name"
let personName = person[nameKey]
Currently, if I do that the compiler thinks that I am trying to access an array, so it asks me for an integer
Thanks
No this will probably never be allowed.
If you want to have dynamic access to a field of a JS object, and given the different fields are homogeneous, you’d better model them as Js.Dict.t<'a>
(https://rescript-lang.org/docs/manual/latest/api/js/dict)
3 Likes
Noted,
I was trying to use it as dynamic access for the object from API.
Anyway, I still able to work around using the switch statement.
Thanks for the reply @tsnobip
May be possible while keeping soundness by using Polymorphic Variants as long as it matches ones of the object keys
type flower = {
color: string,
petals: int
}
let flower = {
color: "red",
petals: 5
}
type flowerKeys = [ #color | #petals ]
let key: flowerKeys = #color
flower[key]