Stream: beginners

Topic: ✔ Static Dispatch as a Abilities replacement


view this post on Zulip EnDeRBeaT (Aug 22 2025 at 13:36):

Hello, I have started learning roc for fun, and I have read static dispatch proposal. I am a bit confused about substituting abilities with it.

Rust allows to impl trait A for type B if either trait or type came from the module the impl block is.

While proposal talks about how you can easily substitute abilities with static dispatch by having a function in the module, there is absolutely nothing that talks about how can I extend existing types when I want to create my ability.

Can you guys tell me how it should go?

view this post on Zulip Anton (Aug 22 2025 at 14:44):

Hi @EnDeRBeaT,
Allowing extension of existing types is not planned. From #ideas > static dispatch - on Str @ 💬 :

In general, methods only get defined in the module of the custom type

Feel free to share a specific problem that you would solve with extension of an existing type and we can discuss how to tackle it instead.

view this post on Zulip EnDeRBeaT (Aug 22 2025 at 15:30):

Hi!
I do not have a concrete example on hand, I just wanted to point out that it doesn't seem like static dispatch can't fully replace abilities.

For example, you would not be able to define a new "ability" (https://www.roc-lang.org/abilities#defining-a-new-ability), because you can't extend other types.

view this post on Zulip Anton (Aug 22 2025 at 16:00):

With the example provided there under "Advanced Topic: Defining a new Ability", you need to control the type definition of Color to add CustomInspect { .... This will work similarly with static dispatch except you need to make a module (file) Color.roc. I believe current abilities do not support the extension of existing types. You can only "extend" existing types if you wrap them in a new type, which will also be possible with static dispatch.

view this post on Zulip EnDeRBeaT (Aug 22 2025 at 16:02):

Anton said:

if you wrap them in a new type

Okay, I think that it might be easier to do in Roc than in Rust, hence there is not a lot of need. Thank you

view this post on Zulip Notification Bot (Aug 22 2025 at 16:02):

EnDeRBeaT has marked this topic as resolved.


Last updated: Sep 09 2025 at 12:16 UTC