Stream: beginners

Topic: ✔ Most basic way to split code across files


view this post on Zulip mkrieger1 (Dec 14 2023 at 10:45):

So far I have only written programs which consist of a single file that is an app. I would like to move some functions into a separate file to be able to reuse them.

I could not find a way to do that so far. From the tutorial, it is not clear to me whether I have to create a package or an interface, or both, and how I can use it in my app. The tutorial links to some examples (Parse Package and Html Interface) on GitHub, but they seem far more complex than what I would like to achieve.

In addition, in several attempts the compiler either got stuck or crashed with e.g. thread 'main' panicked at 'There were still outstancing Arc references to module ids', and I'm unsure whether I've done something wrong, or if this is a compiler bug.

Can someone please show a minimal example of creating one function (e.g. addOne = \x -> x + 1) in a separate file and using it in an app using platform basic-cli, that is supposed to work with roc nightly pre-release, built from commit a187d14 on Mo 04 Dez 2023 09:08:55 UTC?

view this post on Zulip Luke Boswell (Dec 14 2023 at 10:59):

On my mobile so forgive me if the formatting is terrible.

# AddOne.roc
interface AddOne
    exposes [addOne]
    imports []

addOne = \x -> x+1
# main.roc
app ""
    package ... and other header stuff
    imports [ AddOne ]

expect AddOne.addOne 2 == 3

view this post on Zulip Luke Boswell (Dec 14 2023 at 11:03):

This isn't working code you can copy and paste but hopefully shows the key parts. I hope this helps you make some progress.

view this post on Zulip mkrieger1 (Dec 14 2023 at 11:49):

Thanks, this seems as simple as I imagined it should be.

This should definitely be in the tutorial ;)

(I assume it will be part of Interface Modules [This part of the tutorial has not been written yet. Coming soon!])

view this post on Zulip mkrieger1 (Dec 14 2023 at 11:55):

mkrieger1 wrote:

in several attempts the compiler either got stuck

Or my program may have worked and was waiting for input from stdin :flushed: :face_with_peeking_eye:

view this post on Zulip Notification Bot (Dec 14 2023 at 12:00):

mkrieger1 has marked this topic as resolved.

view this post on Zulip mkrieger1 (Dec 14 2023 at 15:06):

This has also been covered in #beginners > ✔ minimal example for separating app code into different ..., but I did not find it until now.

view this post on Zulip Luke Boswell (Dec 14 2023 at 19:39):

I'm glad you figured it out. And yeah, definitely needs to be in the tutorial. :big_smile:

view this post on Zulip Notification Bot (Dec 14 2023 at 20:31):

std power has marked this topic as unresolved.

view this post on Zulip Notification Bot (Dec 15 2023 at 09:28):

mkrieger1 has marked this topic as resolved.

view this post on Zulip Luke Boswell (Dec 15 2023 at 10:46):

Love all the resolved/unresolved posts. I can never figure out what that means. Does it do anything in zulip?

view this post on Zulip Anton (Dec 15 2023 at 11:03):

When resolved, a checkmark is added to the left of the topic in the overview on the left, as well as on the top bar of the discussion.

view this post on Zulip Brendan Hansknecht (Dec 15 2023 at 15:53):

And on web it tells people they are posting to an already resolved topic and asks if they are sure they want to do so.


Last updated: Jul 06 2025 at 12:14 UTC