Stream: beginners

Topic: ✔ Auto-Derived Abilities


view this post on Zulip Bryce Miller (Nov 18 2023 at 22:35):

I have a an opaque type like this:

## A number in the range 1-9, inclusive.
Number := I8 implements [
        Eq { isEq },
    ]

isEq : Number, Number -> Bool
isEq = \@Number a, @Number b -> a == b

Do I remember correctly that there is a way to auto-derive abilities? If not, how would I go about implementing Hash for this type? (I want to put it in a Set)

view this post on Zulip Brendan Hansknecht (Nov 18 2023 at 22:38):

Auto-derive is for non-opaque types. I don't think we have a way to enable an opaque type to use the auto-derived implementation. So a wrapper would be needed.

Just need to implement this ability: https://www.roc-lang.org/builtins/Hash#Hash

And you should be able to just implemented by calling Hash.hash on the inner type wrapped by the opaque.

view this post on Zulip Richard Feldman (Nov 18 2023 at 22:44):

yeah so to auto-derive, you can just say implements [Eq, Hash]

view this post on Zulip Luke Boswell (Nov 18 2023 at 22:45):

Would be great to have an example of an Opaque type with these abilities in https://github.com/roc-lang/examples if anyone feels like writing that up

view this post on Zulip Brendan Hansknecht (Nov 18 2023 at 22:45):

Richard Feldman said:

yeah so to auto-derive, you can just say implements [Eq, Hash]

oh....missed that we added that. awesome.

view this post on Zulip Bryce Miller (Nov 18 2023 at 22:55):

Richard Feldman said:

yeah so to auto-derive, you can just say implements [Eq, Hash]

Yes! Thank you!

view this post on Zulip Notification Bot (Nov 18 2023 at 22:55):

Bryce Miller has marked this topic as resolved.

view this post on Zulip Luke Boswell (Nov 18 2023 at 23:04):

Issue #106 created for this


Last updated: Jul 06 2025 at 12:14 UTC