Stream: beginners

Topic: Syntax for Roc post Abilities and Opaque types


view this post on Zulip Henrik Larsson (Feb 14 2025 at 22:20):

I have not been able to find/understand what the plans are for Roc syntax in a world where Abilities and Opaque types are not longer part of the language. Currently in my project have have places with code like this :

JsonBody a := {
    jsonrpc : Str,
    method : Method,
    params : a,
    id : Str,
}
    implements [Encoding]

This uses both Abilities and Opaque types, in other places I use the support for phantom types in opaque types. I would be glad if someone could show me a code example on how to express that my custom type supports Encoding and also how phantom types would look like in custom types.

view this post on Zulip Brendan Hansknecht (Feb 14 2025 at 22:38):

From what I understand, Custom types and Opaque types are essentially identical. Custom types just enable exposing internals if wanted.

view this post on Zulip Brendan Hansknecht (Feb 14 2025 at 22:38):

So I would mostly view Opaque -> Custom as a rename of a feature with some minor changes

view this post on Zulip Brendan Hansknecht (Feb 14 2025 at 22:39):

They should support phantom types in the exact same way

view this post on Zulip Brendan Hansknecht (Feb 14 2025 at 22:41):

how to express that my custom type supports Encoding

This you can't do. At least not concretely like you could with abilities

In the new static dispatch world, interfaces are automatically implemented. If a custom type exposes functions that match encode, that custom type implements the encode interface. This is essentially identical to how interfaces work in golang (which might help give you bigger context/reference)

view this post on Zulip Brendan Hansknecht (Feb 14 2025 at 22:42):

So you would probably expose all the encode related functions and then write a test that uses your custom type with encode to ensure everything works.

view this post on Zulip Henrik Larsson (Feb 15 2025 at 07:30):

Ok thanks. Cool that interfaces will work as in golang. I had totally missed that.


Last updated: Jul 06 2025 at 12:14 UTC