Stream: beginners

Topic: Dynamic record key name


view this post on Zulip Seth Workman (Mar 14 2023 at 15:42):

Is there a way to use a type alias or something like the following for a record's key name? In this example UserId can be a random Str

UserId : Str

User : {
    UserId : UserDetail
}

view this post on Zulip Wolfgang Schuster (Mar 14 2023 at 15:56):

Wouldn't that be a use case for a hashmap/dict? (I don't recall what it's called in Roc.) Where the UserId is the key and the UserDetail is the value. IIRC records are for fixed keys/values.

view this post on Zulip Brendan Hansknecht (Mar 14 2023 at 16:12):

Yeah, you want a roc Dict

view this post on Zulip Brendan Hansknecht (Mar 14 2023 at 16:13):

Dict UserId UserDetail

view this post on Zulip Brendan Hansknecht (Mar 14 2023 at 16:13):

https://www.roc-lang.org/builtins/dict

view this post on Zulip Anton (Mar 14 2023 at 16:15):

Perhaps I misunderstood but I assumed Seth wanted something like this:

UserId : Str

User : {
    userId : UserId
}

myUser : User
myUser = { userId: "123" }

view this post on Zulip Seth Workman (Mar 14 2023 at 16:16):

So the root of the question comes from trying to decode JSON. Something that looks like this where "userid123" can be a random string of letters/numbers

{
    "users": {
      "userid123": {
        "lastLogin": "2023-03-11T03:38:05.007Z",
      }
    }
}

view this post on Zulip Seth Workman (Mar 14 2023 at 16:18):

I just tried using Dict but Dict does not implement Decoding

view this post on Zulip Wolfgang Schuster (Mar 14 2023 at 16:23):

Can you decode into a List and then convert that to a Dict?

view this post on Zulip Wolfgang Schuster (Mar 14 2023 at 16:26):

Looking at the Dict docs @Brendan Hansknecht linked, I think you'd want something like

Dict.fromList [ T userId userDetails, ..... ]

view this post on Zulip Brendan Hansknecht (Mar 14 2023 at 16:29):

Oh, is your json in a static format? Like always same keys with same types?

view this post on Zulip Brendan Hansknecht (Mar 14 2023 at 16:30):

If so, it should be decoded into a record preferably. If not, a Dict is what you would want. That said, I don't think anyone has written code for decoding something into a dictionary or similar.

view this post on Zulip Seth Workman (Mar 14 2023 at 16:33):

hmm ok. I'll try and play around with it some more later and see if I can come up with something

view this post on Zulip Wolfgang Schuster (Mar 14 2023 at 16:38):

Decoding to a List first and then calling Dict.fromList seems fairly reasonable as glancing at how Elm's Json.Decode.dict works this is what it does.

view this post on Zulip Seth Workman (Mar 14 2023 at 19:43):

I wasn’t able to conjure something up for this…

view this post on Zulip Wolfgang Schuster (Mar 14 2023 at 19:53):

Are you able to decode to a List? I don't have a Roc setup in front of me at the moment so trying to do this all in my head :sweat_smile:

view this post on Zulip Brendan Hansknecht (Mar 14 2023 at 20:05):

@Luke Boswell you were recently working on json decoding right? Any examples you can share and ideas how this would work out. I haven't messed with json decoding yet and don't know how it looks on roc.

view this post on Zulip Seth Workman (Mar 14 2023 at 20:39):

Wolfgang Schuster said:

Are you able to decode to a List? I don't have a Roc setup in front of me at the moment so trying to do this all in my head :sweat_smile:

I was not, I'm familiar enough with roc yet . This was my first mini project to see if I could convert one of my node scripts into roc

view this post on Zulip dank (Mar 14 2023 at 20:43):

Seth Workman said:

Wolfgang Schuster said:

Are you able to decode to a List? I don't have a Roc setup in front of me at the moment so trying to do this all in my head :sweat_smile:

I was not, I'm familiar enough with roc yet . This was my first mini project to see if I could convert one of my node scripts into roc

maybe this will help https://github.com/roc-lang/roc/blob/main/examples/python-interop/platform/main.roc

view this post on Zulip Luke Boswell (Mar 14 2023 at 20:51):

@Seth Workman I made an example in this zulip which has decoding json into a record.

view this post on Zulip Luke Boswell (Mar 14 2023 at 20:53):

It works, but it can be pretty fragile. There are at least a few decent bugs lurking in the Json builtin. I recently added the expects, so now I'm more confident I know how to test it, the next step is to find more of these bugs and iron them out. If you do find something that breaks, please log an issue that would be very helpful.

view this post on Zulip Seth Workman (Mar 14 2023 at 20:54):

Yea I'm able to get basic json decoding working. But i can't get it working for my use case as my json doesn't have static key names

view this post on Zulip Luke Boswell (Mar 14 2023 at 20:55):

Note that URL json package is a little old now, so recommend using the one in main at roc/crates/compiler/builtins/roc etc

view this post on Zulip Luke Boswell (Mar 14 2023 at 20:55):

Ah, I see...

view this post on Zulip Luke Boswell (Mar 14 2023 at 20:56):

I'm not sure if that is supported yet. I haven't spent enough time looking inside the Json module. I can have a look later this evening.

view this post on Zulip Seth Workman (Mar 14 2023 at 20:58):

Alright no worries! Thanks for some insight

view this post on Zulip Richard Feldman (Mar 14 2023 at 22:14):

@Luke Boswell it should be possible to make the JSON format decode directly into a Dict and have it Just Work as long as the Dict keys and values both have Decode

view this post on Zulip Luke Boswell (Mar 14 2023 at 22:24):

decode directly into a Dict and have it Just Work

That's awesome!! Looking forward to testing it out.

view this post on Zulip Richard Feldman (Mar 14 2023 at 22:34):

but it will require adding Decode to Dict - it doesn't have it yet

view this post on Zulip Richard Feldman (Mar 14 2023 at 23:17):

oh that might be blocked on having the feature of "give this Decode iff its k and v type variables also have Decode" :thinking:

view this post on Zulip Ayaz Hafiz (Mar 15 2023 at 01:36):

that feature already ezists

view this post on Zulip Ayaz Hafiz (Mar 15 2023 at 01:37):

It will automatically be inferred as appropriate in either manual or derived implementations


Last updated: Jul 06 2025 at 12:14 UTC