Stream: beginners

Topic: expose a value from an interface


view this post on Zulip Jim Balhoff (Apr 03 2023 at 16:54):

I have an interface defined something like this:

interface LinkedList
    exposes [
        LinkedList,
        single,
        push,
        pop,
        toList,
        concat,
        walk,
        empty,
    ]
    imports []

LinkedList a := LL a

LL a : [Nil, Cons { first : a, rest : LL a}]

empty : LinkedList *
empty = @LinkedList Nil

# and some more stuff...

When I use empty in another module, it crashes roc build. If I redefine empty as a function (and call it that way), it works:

empty : {} -> LinkedList *
empty = \_ -> @LinkedList Nil

Is it supposed to be possible to expose a value, or only functions?

view this post on Zulip Jim Balhoff (Apr 03 2023 at 17:06):

I see I can expose other kinds of values—maybe this is a problem with the LinkedList * type? In Scala I would have made this LinkedList[Nothing] which is a subtype of every type. I guess * doesn't work that way?

view this post on Zulip Jim Balhoff (Apr 03 2023 at 17:10):

Oh and I see my function version is not working anyway when I try to actually add an item to the empty list.

view this post on Zulip Brendan Hansknecht (Apr 03 2023 at 17:43):

I don't recall the details, but i know we had to do the same thing with dictionaries.

view this post on Zulip Brendan Hansknecht (Apr 03 2023 at 17:44):

Due to exposing a Dict k v, empty is required to be a function.

view this post on Zulip Brendan Hansknecht (Apr 03 2023 at 17:44):

It is related to constants and type parameters, but i don't understand the full details

view this post on Zulip Brendan Hansknecht (Apr 03 2023 at 17:47):

Given the function version works with built-in types and interfaces, it should work with your linked list example. Would need to see more code to make a guess at the issue when adding a value.

view this post on Zulip Jim Balhoff (Apr 03 2023 at 18:07):

Thanks - I will try to look at the Dict version and see how the types are written

view this post on Zulip Fabian Schmalzried (Apr 04 2023 at 11:00):

In case you want the full background of why Dict.empty (and your LinkedList.empty) have to be functions: https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/Let-generalization.20-.20let's.20not.3F/near/318649802


Last updated: Jul 06 2025 at 12:14 UTC