Maps in statically typed languages require all the values to be the same type, though, unlike structs.
That's only a problem if a language doesn't have sum types. If we use OCaml again, with the person_info discriminated union above, it's easy to make a map with different types:
type person_info = Name of string | Age of int
module Person = Map.Make(String)
let p =
|> Person.add "name" (Name "Baby Yoda")
|> Person.add "age" (Age 50)
Person.find "name" p
- : person_info = Name "Baby Yoda"
Person.find "age" p
- : person_info = Age 50
Ooh. I don't know OCaml, but that looks interesting. I guess I could probably implement something similar in Haskell.
Exactly the same principle in Haskell:
import qualified Data.Map.Strict as Map
data PersonInfo = Name String | Age Integer
m = Map.fromList [("name", Name "Baby Yoda"), ("age", Age 50)]
Map.lookup "name" m
Just (Name name) = Map.lookup "name" m
-- => "Baby Yoda"
Some static languages have reflections and libraries for dynamism where you need them.
Scala have dynamic trait.
Haxe have dynamic types.
Java have reflection and libs to help.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.