Stream: beginners

Topic: Module Names


view this post on Zulip Fletch Canny (Jan 11 2024 at 11:40):

Hi everyone!
I have a few questions about modules

  1. Can I rename modules like in Elm/Haskell/Purescript?
import ModuleName as M
  1. Is there a way to have name a module something other than it's directory for this specific case-
    I have a collection of modules, let's call them A, A.B, A.C, A.D. B, C, D are all sub-modules of A, and in my file hierarchy I would like to have them in the same file. Other languages I've seen (like Rust) will have the A module file be called something generic (like mod.rs) within the file A, and all the other files work exactly as they do in Roc.

Thanks!

view this post on Zulip Folkert de Vries (Jan 11 2024 at 13:13):

roc modules always correspond to a file. That makes file loading faster and error messages more reliable

view this post on Zulip Folkert de Vries (Jan 11 2024 at 13:15):

the as Foo should work I think? if not that's a bug (but imports are getting reworked anyway right now to allow them in more places, so if it's a bug we likely won't fix it and rather land the new approach)

view this post on Zulip Fletch Canny (Jan 11 2024 at 13:23):

Folkert de Vries said:

roc modules always correspond to a file. That makes file loading faster and error messages more reliable

I still feel like a special case for module names would be nice, since it makes file management a bit easier.
Is there something in the compiler which prevents swapping from a file to a directory for this case? Either performance-wise or maybe this is a bad design idea? Because I feel like that wouldn't be much pre-processing needed to make it happen.

I'm not sure though, happy to change my mind.

Folkert de Vries said:

the as Foo should work I think? if not that's a bug (but imports are getting reworked anyway right now to allow them in more places, so if it's a bug we likely won't fix it and rather land the new approach)

Oh you're supposed to be able to do that? I'll update my compiler and see if that fixes it. It was assuming I was referring to a file name before.

view this post on Zulip Folkert de Vries (Jan 11 2024 at 13:58):

hmm, that might be because we also support including files verbatim as string/list of byte

view this post on Zulip Folkert de Vries (Jan 11 2024 at 13:58):

@Agus Zubiaga might know more about the current/future state of imports

view this post on Zulip Fletch Canny (Jan 11 2024 at 13:59):

Yeah that's a cool feature, which I use often.

view this post on Zulip Agus Zubiaga (Jan 11 2024 at 14:52):

The as keyword in imports is currently not supported, but it's introduced in the #ideas>module and package changes proposal. I'm currently working on it. The new imports are pretty close to done, but we're probably going to hold that until the rest of the proposal is implemented, to prevent multiple batches of breaking changes.

view this post on Zulip Agus Zubiaga (Jan 11 2024 at 14:56):

Note the syntax in the doc was revised in Zulip later. The most up-to-date one looks like this:

import Json as J exposing [int, string]
import "names.txt" as names : Str

The as and exposing keywords are optional, and imports can appear nested inside defs/expressions so they can be introduced in a smaller scope than the entire module.

view this post on Zulip Agus Zubiaga (Jan 11 2024 at 15:01):

See also #ideas > module params

view this post on Zulip Fletch Canny (Jan 11 2024 at 15:02):

Thanks for working on this change! I like it, especially coming from other ML family languages. I do have a few questions though-

Will Roc support rust-style nested imports? That syntax seems like it might make that tricky.

Also, if we're not allowed to have mod.rs-like files, is there a consensus about what to name a file if you keep it inside a folder of the same name? The Parser library by @Luke Boswell uses Core which I like.

view this post on Zulip Agus Zubiaga (Jan 11 2024 at 15:13):

Fletch Canny said:

Will Roc support rust-style nested imports? That syntax seems like it might make that tricky.

Check out the "Module and package privacy changes" section of the proposal, there a few changes that are probably better explained there :smile:

view this post on Zulip Fletch Canny (Jan 11 2024 at 15:13):

oh haha I missed that. I'll check it out

view this post on Zulip Agus Zubiaga (Jan 11 2024 at 15:14):

Note import X from Y becomes import Y exposing X after syntax modification decisions that are not updated in the doc

view this post on Zulip Notification Bot (Jan 11 2024 at 18:11):

Simon Peleška has marked this topic as resolved.

view this post on Zulip Notification Bot (Jan 11 2024 at 18:11):

Simon Peleška has marked this topic as unresolved.


Last updated: Jul 05 2025 at 12:14 UTC