Stream: beginners

Topic: How to import interface from another directory?


view this post on Zulip Zellyn Hunter (Dec 17 2023 at 02:28):

The examples of interface packages I was able to find place the interface file for interface Foo in Foo.roc, and then add Foo to imports. How does one do that when you want to move Foo.roc to another directory?

view this post on Zulip Brendan Hansknecht (Dec 17 2023 at 05:17):

Haven't tested, but I thought you just used an extra name capitalized name with dot syntax.

imports [Dir.Foo]

view this post on Zulip Brendan Hansknecht (Dec 17 2023 at 05:17):

And it may be the case the Dir has to start with a capital letter

view this post on Zulip Brendan Hansknecht (Dec 17 2023 at 05:18):

But that is just a guess from memory and it may not even be implemented, not sure

view this post on Zulip Luke Boswell (Dec 17 2023 at 07:17):

Yeah that's right

view this post on Zulip Zellyn Hunter (Dec 17 2023 at 17:10):

So can I do imports [../../Dir.Foo]? In my Go adventofcode code, I arrange things like so:

Helper code lives in top-level directories:

Solution code lives in date-named directories:

I wanted to put Charmap.roc in /charmap/Charmap.roc and somehow let my day 14 solution in /2023/14/main.roc import it…

view this post on Zulip Luke Boswell (Dec 17 2023 at 17:35):

The key thing here is that the directory has to start with a capital letter. So numbers and lowecase letters wont work because they are not valid identifiers in Roc.

view this post on Zulip Zellyn Hunter (Dec 17 2023 at 17:39):

So do you need symlinks if you want to refer to something up the path hierarchy?

view this post on Zulip Brendan Hansknecht (Dec 17 2023 at 17:42):

Packages can use paths

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

So if charmap is a full package, you should just use a path to it's main file

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

If it is just an interface, it has to be equal directory or deeper

view this post on Zulip Zellyn Hunter (Dec 17 2023 at 18:55):

Ah, ok. The tutorial left me completely confused on the difference between packages and interfaces. I saw a recommendation here that packages are expected to come from far away, and be downloaded when necessary. But equal-or-deeper means that both (a) organizing your code into subdirectories and (b) having a set of useful interfaces for that codebase, is impossible, and you’d need to use packages even for nearby code.

view this post on Zulip Zellyn Hunter (Dec 17 2023 at 18:55):

Seems easy enough to do (lots of code has the platform in the same repo as code and uses “../../platform” to use it.

view this post on Zulip Zellyn Hunter (Dec 17 2023 at 18:56):

Which, again, leaves me mystified what interfaces are for :smile:

view this post on Zulip Brendan Hansknecht (Dec 17 2023 at 19:00):

Probably interfaces should be more flexible. But they are common within a single app for multiple files or within a package

view this post on Zulip Zellyn Hunter (Dec 17 2023 at 19:01):

What are they for, though? Are they the only way to split code between files locally to a directory?

view this post on Zulip Brendan Hansknecht (Dec 17 2023 at 19:04):

Splitting up code, giving a nice public interface, name spacing.

view this post on Zulip Brendan Hansknecht (Dec 17 2023 at 19:08):

Taking a look at a real package example may help. This is the code for roc-pg a Postgres driver:
https://github.com/agu-z/roc-pg/tree/main/src

It has one package in main and everything else is an interface.

view this post on Zulip Zellyn Hunter (Dec 17 2023 at 19:08):

Thanks, I’ll take a look when I get home!

view this post on Zulip Zellyn Hunter (Dec 18 2023 at 03:36):

Aaah! That answers my questions! Pg.Batch(/Pg/Batch.roc) imports Batch (/Batch.roc) with no trouble - the names are based on the root!


Last updated: Jul 06 2025 at 12:14 UTC