Stream: beginners

Topic: What will interfaces / traits look like?


view this post on Zulip Ben (Feb 11 2025 at 00:43):

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?

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 00:51):

There would be no dynamic dispatch

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 00:51):

you would need to make Shape a tag union and dispatch from the tag

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 00:54):

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.

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 00:55):

The print_area function would be:

print_area! : a => () where a.area() -> F64
print_area! = |shape|
    Stdout.line!(shape.area().to_str())

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 00:56):

not 100% sure on the syntax, but roughly that

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 00:57):

The interfaces are all implicit. If any custom type expose area: Self -> F64, it automatically can be used with the above function

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 00:58):

You also can name an interface if you want:

Shape a : a where a.area() -> F64
print_area! : Shape a => ()

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 00:59):

Does that answer your question?

view this post on Zulip Ben (Feb 11 2025 at 04:32):

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?

view this post on Zulip Luke Boswell (Feb 11 2025 at 04:33):

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

view this post on Zulip Ben (Feb 11 2025 at 04:37):

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

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 04:38):

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

view this post on Zulip Ben (Feb 11 2025 at 04:41):

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

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 04:43):

Yes

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 04:44):

Also, I coded in go professionally for a few years and never ran into the problem. I think it is generally very rare.

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 04:44):

If I ran into it in roc, I would just convert one custom type into another custom type for the less used interface

view this post on Zulip Brendan Hansknecht (Feb 11 2025 at 04:45):

thing.abc() and thing.to_interface().abc()


Last updated: Jul 06 2025 at 12:14 UTC