Does roc plant to have, or currently have anything that allows for optional/ default arguments in functions or function overloading?
I ask this because having used both fsharp and ocaml I think the single feature I like most about ocaml over fsharp is that named function args allow for a function to have optional parameters with a default value.
I'll now try to explain why I think this is good incase roc has better/other solutions to these problems:
I think they provide the best solution I've seen for allowing testing functional code that has dependencies.
Basically one pattern I think it's very useful for is:
I have a function but I want to test it.
It makes some logical assumptions about how it should be used you can use optional args to slow changing that, but still having usual usage be simple.
Slightly contrived example below:
Say it's a "console.prettyprint" function that does some fancy formatting. Well you want to write to the console ordinarily... To test it you actually don't want to write to console, you want to write to a test buffer. So you have an optimal arg which is 'writefunc' string-->unit , we default it to console.writeline. Then you can override it in testing, or if you just want to use the function in some other way.
Basically it encourages making functions very generic without having the caller pay a price for that genericness. By making them constantly pass in the same args in 95% of use cases and let's them override it the 5% of the time they want to use it differently.
Obviously function overloading provides the same features, but it think ocaml's optionals are more robust.
I'm probably the least experienced person here with Roc, but you'd do that by having the function accept a record with optional record fields, which has the benefit of decoupling optional arguments (and their default values) from the syntax of function calls. You can find more details in the Roc tutorial page, a little bit within the Records subsection.
To my understanding, at least
https://www.roc-lang.org/tutorial#records
Yeah, alright, that'll do nicely :).
I knew about optimal fields but hadn't made the leap that you could define one in a function definition.
Thanks a lot @Declan Joseph Maguire
The tutorial actually shows it being used in a lambda, with the record being anonymously defined by its fields. As for overloading, I think it depends on what you consider counts as overloading? But I think in the broadest sense it's explicitly something Roc won't support because every def will have one and only one value within a scope. I think though you can do branching on types within a function and the compiler should optimise that all appropriately due to the Hindley-Milner principle type inference
@Declan Joseph Maguire yeah... I realised that :face_palm:. In my defence, it's been quite a while since I fully read the tutorial.
I don't care about overlapping itself per-say just an efficient, low overhead way to provide optional arguments to functions, overloading being a common one.
To be fair you're now making me realise I don't know how to patten match on type. I think I always assumed that because Tags and Types are both capitalised in Roc that they'd be related?
This is actually the reason optional record fields were added to the language!
Last updated: Jul 05 2025 at 12:14 UTC