I understand Abilities will be removed after adding static dispatch. Can someone explain what a type implementing an interface might look like? Maybe a roc equivalent of this example in Rust https://rustbyexample.io/traits
Will there be any support for dynamic dispatch? Can I have a List Shape
or would I wrap it in an enum first?
There would be no dynamic dispatch
you would need to make Shape
a tag union and dispatch from the tag
In static dispatch, you would have 3 files Rectangle.roc
, Circle.roc
, and main.roc
Rectangle and Circle would both create custom types (not sure what the new syntax will be for that). They would also expose an area
function with the first arg being their type.
The print_area
function would be:
print_area! : a => () where a.area() -> F64
print_area! = |shape|
Stdout.line!(shape.area().to_str())
not 100% sure on the syntax, but roughly that
The interfaces are all implicit. If any custom type expose area: Self -> F64
, it automatically can be used with the above function
You also can name an interface if you want:
Shape a : a where a.area() -> F64
print_area! : Shape a => ()
Does that answer your question?
Are you able to disambiguate if there are two different "trait functions" with the same name? and what does the syntax look like if area takes Self and another param?
Roc will dispatch to the module where the Custom type is defined and call the function with that name. I'm not sure you can even have two Custom types in the same module... but if you could and they had the same name that would be ambiguous
I see, I'm thinking suppose there were two libraries, and they both happened to have an API where you can pass in a type that must have a function called frobnicate()
but expect different implementations
and I want to be able to use my single type with both libraries
This is like go interfaces. If you have a function with the correct name and type signature, you match the interface whether you want to or not
Gotcha so I guess it's a lot of the same answerse as here https://stackoverflow.com/questions/72019486/how-to-implement-two-interfaces-with-same-method-name-and-different-arguments
Yes
Also, I coded in go professionally for a few years and never ran into the problem. I think it is generally very rare.
If I ran into it in roc, I would just convert one custom type into another custom type for the less used interface
thing.abc()
and thing.to_interface().abc()
Last updated: Jul 06 2025 at 12:14 UTC