Stream: beginners

Topic: ✔ minimal example for separating app code into different ...


view this post on Zulip Becker A. (Dec 03 2023 at 18:14):

I'm working through AoC and I want to split off my parsing code from my main.roc file, so that I can separate my puzzle logic from my input logic. I tried making different combinations of package and interface modules, but regardless I keep getting compile errors (mostly around my "imports" lines, for whatever reason).

-> what's a minimal example of a Roc app module that pulls code from other local modules?

my latest attempt at a minimal e.g.:

file structure:

├── deps
│   ├── ParsedText.roc
│   └── main.roc
├── input.txt
└── main.roc

main.roc:

app "day2"
    packages {
        pf: "https://github.com/roc-lang/basic-cli/releases/download/0.7.0/bkGby8jb0tmZYsy2hg1E_B2QrCgcSTxdUlHtETwm5m4.tar.br",
        deps: "deps/main.roc",
    }
    imports [
        pf.Stdout,
        deps.ParsedText.{ text },
    ]
    provides [main] to pf

main =
    Stdout.line text

deps/main.roc:

package "deps"
    exposes [ParsedText]
    packages {
        # I also want this code to depend on an external package, w/o having to pollute my app code's dependencies
        parse: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.3.0/-e3ebWWmlFPfe9fYrr2z1urfslzygbtQQsl69iH1qzQ.tar.br",
    }

ParsedText.roc:

interface ParsedText
    exposes [text]
    imports [
        parse.parseStr,
        parse.string,
    ]

text = parseStr (string "text") "text" == Ok "text"

compile error for roc build:

── WEIRD IMPORTS ───────────────────────────────────────── deps/ParsedText.roc ─

I am partway through parsing a imports list, but I got stuck here:

2│      exposes [text]
3│      imports [
4│          parse.parseStr,
            ^

I am expecting a comma or end of list, like

    imports [Shape, Vector]%

... do I not have a comma? I'm not sure what to do here.

view this post on Zulip Luke Boswell (Dec 03 2023 at 18:19):

Do you want to make a package or just separate into another module? If you move the ParsedText.roc to the same folder as your main.roc you can just directly import it, i.e. remoce the deps. prefix.

view this post on Zulip Luke Boswell (Dec 03 2023 at 18:21):

It looks like you wamt to use a package to keep the parser dep separate. I'm not sure about how to do that, I tried a bit with my aoc-template and ended up just vendoring the Parser package code.

view this post on Zulip Luke Boswell (Dec 03 2023 at 18:22):

https://github.com/lukewilliamboswell/aoc/blob/main/src/S2023/D02.roc

view this post on Zulip Luke Boswell (Dec 03 2023 at 18:23):

If you look at my day 2 you can see how I have achieved that, but it feels like a workaround.

view this post on Zulip Brian Carroll (Dec 03 2023 at 19:05):

Yeah a "package" module is supposed to be something loaded from far away, like the package manager that doesn't exist yet!
Within your own app you only need interface modules.

view this post on Zulip Becker A. (Dec 03 2023 at 19:43):

so remove the "package" bit and just use interfaces, got it!

yeah I don't mind too much if my top-level app module has to list roc-parser as a dependency, I can live with that. as long as I can access it from my interface that's fine for now.

lemme give it a go and see what happens... :fingers_crossed:

view this post on Zulip Luke Boswell (Dec 03 2023 at 19:51):

Yeah that works. If your app imports the package. The interface modules can import it to woth the same prefix I think

view this post on Zulip Becker A. (Dec 03 2023 at 19:56):

yeah I'm still getting errors T-T

code is in this GH-Gist (I forgot about gists, this is way cleaner :sweat_smile:)
https://gist.github.com/CrepeGoat/222803ee7bc1ff665fe679d8e96c9018

error:

── WEIRD IMPORTS ────────────────────────────────────────────── ParsedText.roc ─

I am partway through parsing a imports list, but I got stuck here:

2│      exposes [text]
3│      imports [
4│          parse.parseStr,
            ^

I am expecting a comma or end of list, like

    imports [Shape, Vector]%

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 20:06):

Do we have different parsing for imports in interfaces? That would be surprising.

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

I think you still need the module name so parse.Core

view this post on Zulip Luke Boswell (Dec 03 2023 at 20:12):

Or parse.Core.{ parseStr, ...etc}

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

Also unrelated but you wont be able to print text directly to Stdout as it's a Bool. Maybe just use dbg and roc dev, and print an empty string or something.

view this post on Zulip Becker A. (Dec 03 2023 at 20:14):

Luke Boswell said:

Also unrelated but you wont be able to print text directly to Stdout as it's a Bool. Maybe just use dbg and roc dev, and print an empty string or something.

yeah I'm seeing that now too, thanks!

view this post on Zulip Becker A. (Dec 03 2023 at 20:17):

Luke Boswell said:

I think you still need the module name so parse.Core

that made the file parse-able! thanks

though the error message I got for that was really unhelpful. tbh I still don't know why specifying a wrong path for a dependency (i.e., a semantic issue) prevented the entire file from being parsed or formatted (i.e., behaved like a syntax issue). is this worth filing a bug over?

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

That sounds like progress :fingers_crossed:

view this post on Zulip Luke Boswell (Dec 03 2023 at 20:19):

I think it would be helpful to document it. We can add context Re module params development which will replace a lot of the underlying implementation, and almost certainly resolve this issue.

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

Also good to link to when someone else runs into this issue. I did yesterday, but just pressed on instead of stopping to question if I should.

view this post on Zulip Becker A. (Dec 03 2023 at 20:22):

haha yeah AoC this year is a lot more bug-filing and typo-fixing than I remember in past years :joy: I'll write something up and post the link here. thanks again!

view this post on Zulip Becker A. (Dec 03 2023 at 20:31):

OOOHH weird! so if I take my old broken example and capitalize my imports, it then parses and gives me a semantic error.

e.g. can't parse and errors with ~"bad import syntax. add a comma?":

interface ParsedText
    exposes [text]
    imports [
        parse.parseStr,
        parse.string,
    ]

can parse but errors with ~"can't find that module":

interface ParsedText
    exposes [text]
    imports [
        parse.ParseStr,
        parse.String,
    ]

view this post on Zulip Brendan Hansknecht (Dec 03 2023 at 20:33):

That makes sense and is probably super useful for a repro in an issue.

It expects package.Module. So the first is lowecase and the second capitilized.

view this post on Zulip Becker A. (Dec 03 2023 at 20:59):

issue posted here: https://github.com/roc-lang/roc/issues/6172

thanks all!

view this post on Zulip Notification Bot (Dec 03 2023 at 20:59):

Becker A. has marked this topic as resolved.

view this post on Zulip Luke Boswell (Dec 04 2023 at 20:06):

@Richard Feldman @Agus Zubiaga related question, should it be possible to import a package into another package? is this currently implemented?

view this post on Zulip Agus Zubiaga (Dec 04 2023 at 22:19):

I’m afk but I think you can just use the packages keyword in the package header like you do for apps. I don’t remember if I have done this before, though.

view this post on Zulip Agus Zubiaga (Dec 04 2023 at 22:21):

Looks like I have according to this issue’s description: https://github.com/roc-lang/roc/issues/5477

view this post on Zulip Agus Zubiaga (Dec 04 2023 at 22:22):

You will probably run into that but you can workaround it by importing the transitively used modules in the app

view this post on Zulip Agus Zubiaga (Dec 04 2023 at 22:22):

This is one of the main issues I want to fix as part of module params

view this post on Zulip Richard Feldman (Dec 05 2023 at 06:17):

yeah tl;dr it should be possible but there's a bug currently :big_smile:

view this post on Zulip Luke Boswell (Dec 05 2023 at 18:48):

Sounds good. This will mean we can use roc-lang/unicode things in roc-parser. :grinning_face_with_smiling_eyes:

view this post on Zulip mkrieger1 (Dec 18 2023 at 09:35):

See also #beginners > How to import interface from another directory?


Last updated: Jul 06 2025 at 12:14 UTC