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
you cannot add fields to records. Records are static, you cannot dynamically add fields. Something like a Dict
might be a better fit
ok can I pattern match on dict ? for example if it has specific keys in place, if so what is the syntax
you can match on Dict.get yourDict yourKey
but in a dict, all fields must have the same type, that is a restriction there
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?
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.
ok I used unions and it works but still weird : )
incidentally, dynamic languages like Python use tags under the hood - they're just syntactically invisible
like in Python when you write ["foo", 5, someBoolean]
in Roc you would write [Str "foo", Num 5, Bool someBoolean]
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)
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
the pros/cons as I see them are:
type()
or isinstance()
somewhere in order to do something different based on what you had in there)tradeoffs! :smiley:
Last updated: Jul 06 2025 at 12:14 UTC