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
?
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
This isn't working code you can copy and paste but hopefully shows the key parts. I hope this helps you make some progress.
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!])
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:
mkrieger1 has marked this topic as resolved.
This has also been covered in #beginners > ✔ minimal example for separating app code into different ..., but I did not find it until now.
I'm glad you figured it out. And yeah, definitely needs to be in the tutorial. :big_smile:
std power has marked this topic as unresolved.
mkrieger1 has marked this topic as resolved.
Love all the resolved/unresolved posts. I can never figure out what that means. Does it do anything in zulip?
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.
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