Stream: beginners

Topic: Adding methods to pre-existing types


view this post on Zulip misterdrgn (Feb 08 2026 at 16:25):

As I understand it, under the new version a “method” is simply a function defined in the same module as the type of its first argument. These can be called via dot chaining. In contrast, a function defined outside of the module cannot, although it can be chained via a similar syntax (last I heard, using an arrow ->). Thus, Roc distinguishes between functions defined together with a type and those defined later, unlike several other languages that support dot chaining (Swift, Nim, Lean). I’m sure this has advantages and disadvantages.

I’m curious though about how it affects generic functions with type constraints. It seems like you can constrain a type based on its methods, but not based on functions defined outside its module. Is that right? Does that mean there’s no clean way to add methods to a pre-existing type so that a generic function requiring those methods can be called on it?

Thanks.

view this post on Zulip Brendan Hansknecht (Feb 08 2026 at 18:11):

I think you would need a wrapper type that works to create the interface you want

view this post on Zulip Brendan Hansknecht (Feb 08 2026 at 18:11):

So an explicit level of translation

view this post on Zulip misterdrgn (Feb 08 2026 at 19:48):

Thanks, that’s what I thought. I wonder how often in practice people will be tempted to create wrapper types, or even create their own versions of existing modules so that they can add functionality. I don’t know how difficult (or non-idiomatic) it would be to shadow an existing module with your own version, like you can in a language like Ocaml.

view this post on Zulip Brendan Hansknecht (Feb 09 2026 at 01:31):

I think you hit the exact same in go with their interfaces and that ecosystem is fine.

view this post on Zulip Brendan Hansknecht (Feb 09 2026 at 01:31):

So I'm not too worried

view this post on Zulip misterdrgn (Feb 09 2026 at 02:38):

Got it, thanks. Yeah, does look similar in Go. They have struct embedding to help with that, but I dunno how much people use it.


Last updated: Feb 20 2026 at 12:27 UTC