Hi everyone!
I have a few questions about modules
import ModuleName as M
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!
roc modules always correspond to a file. That makes file loading faster and error messages more reliable
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)
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.
hmm, that might be because we also support including files verbatim as string/list of byte
@Agus Zubiaga might know more about the current/future state of imports
Yeah that's a cool feature, which I use often.
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.
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.
See also #ideas > module params
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.
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:
oh haha I missed that. I'll check it out
Note import X from Y
becomes import Y exposing X
after syntax modification decisions that are not updated in the doc
Simon Peleška has marked this topic as resolved.
Simon Peleška has marked this topic as unresolved.
Last updated: Jul 05 2025 at 12:14 UTC