Can we add more key values in record?

Suppose I have a record like this
a = {name= “cam”}

Now I want to add more keywords in that record? is it possible?

So far I have seen that I can update this. But I’m not able to find anything which can add too.

Coming from javascript record is something similar to object

var obj = {key1: value1, key2: value2};

If I have a object I can add it like this

obj.key3 = "value3";

Records in Elm aren’t quite like JavaScript objects; every property you want to add to an Elm record need to be present from when the record is first constructed, such that the record always has the same “shape” if you will. You can use a Dict in Elm though, which allows you to add and remove key-value pairs as you like :slightly_smiling_face:

Oh I think it is similar to Struct in Elixir. Which has set of known fields and you have to use map to manipulate that struct. I would look into Dict. Thanks

You can always extract the fields of a record and construct a new record with more fields. It will be a new type however.

foo : { a : String } -> String -> { a : String, b : String }
foo r b = { a = r.a, b = b }
1 Like

Why do you want to add a new field to a record?

I thought it would be possible. Just learning

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.