colors = %{primary: "red"}
colors.primary.primary
"red"
# regular assignment
primary_color = colors.primary
%{secondary: primary_color} = colors
- how to update the value inside the map?
# it throughs errors
color = %{primary: "red"}
color.primary = "blue" # not works!! because one can't change the data structure!
# so, let's recreate by copying, should use "map" library.
# "put" function is the one to go
Map.put(colors, :primary, "blue")
%{primary: "blue"} # actually you just created the new one. not updated.
colors
%{primary: "red"} # so, it's not updated.
%{color | primary: "blue"} # This is the syntax to create (still not update!)
%{primary: "blue"}
colors
%{primary: "red"} # so, it's not updated.
%{color | secondary: "yellow"} # It throws errors because it doesn't exit.
Map.put(colors, :secondary_color, "yellow")