Stream: beginners

Topic: how records works in roc?


view this post on Zulip Artur Swiderski (Apr 16 2023 at 12:07):

It seems that I want to call function recursively, one parameter being a record, I want to alter this record as I pass it down the line (by adding field etc.) like it was list. At the end I wanted to make certain action depending on pattern matched on specific record produced by this function. Apparently entire scheme is impossible because function seems to accept very specific record and I can't alter that. This harms flexibility since I don't know what kind of record I need before running function. This restrictions seems artificial, what harm it can cause, to add such capability. How to overcome it using existing syntax

view this post on Zulip Folkert de Vries (Apr 16 2023 at 12:09):

you cannot add fields to records. Records are static, you cannot dynamically add fields. Something like a Dict might be a better fit

view this post on Zulip Artur Swiderski (Apr 16 2023 at 12:19):

ok can I pattern match on dict ? for example if it has specific keys in place, if so what is the syntax

view this post on Zulip Folkert de Vries (Apr 16 2023 at 12:21):

you can match on Dict.get yourDict yourKey

view this post on Zulip Folkert de Vries (Apr 16 2023 at 12:21):

but in a dict, all fields must have the same type, that is a restriction there

view this post on Zulip Artur Swiderski (Apr 16 2023 at 12:45):

ok I can use dict , but I will require tweaking. It seems weird though, in some other languages I can throw garbage I have at hand to any structure or class using polymorphism, or if this is not possible, there exist some kind of structure like python dict working with various types. Lacking that capability is limiting (in serious way) is there any roadmap to create some kind of garbage friendly structure like dict in python?

view this post on Zulip Brian Carroll (Apr 16 2023 at 13:07):

No. It sounds like you're used to dynamically typed languages. Statically typed languages require you to learn different ways to write your programs. You can still do everything you can do in dynamic languages, you just need to approach it differently. The reward is faster code and easier refactoring.
As I mentioned in one of the other threads, you can use tagged unions to combine different types into one type. They're covered in the tutorial.

view this post on Zulip Artur Swiderski (Apr 16 2023 at 13:34):

ok I used unions and it works but still weird : )

view this post on Zulip Richard Feldman (Apr 16 2023 at 13:42):

incidentally, dynamic languages like Python use tags under the hood - they're just syntactically invisible

view this post on Zulip Richard Feldman (Apr 16 2023 at 13:43):

like in Python when you write ["foo", 5, someBoolean] in Roc you would write [Str "foo", Num 5, Bool someBoolean]

view this post on Zulip Richard Feldman (Apr 16 2023 at 13:44):

but Python is tagging them behind the scenes; that's type() and isinstance() know at runtime what the type is (they look at the hidden tag)

view this post on Zulip Richard Feldman (Apr 16 2023 at 13:45):

whereas in Roc instead of using type() or isinstance() you'd do a pattern match like:

when myListElement is
    Str str -> # do something with this string
    Num num -> # do something with this number
    Bool bool -> # do something with this bool

view this post on Zulip Richard Feldman (Apr 16 2023 at 13:46):

the pros/cons as I see them are:

view this post on Zulip Richard Feldman (Apr 16 2023 at 13:46):

tradeoffs! :smiley:


Last updated: Jul 06 2025 at 12:14 UTC