Importing multiple files

Hi, is it possible to split code between multiple files? Right now my data model is in one file and it’s becoming quite unwieldy at nearly 2000 lines of code.

I noticed that the default in the compiler is looking for a lib.mo file

image

Could this lib.mo import a load of other files in the same directory under the same namespace? Or is there a better way.

I think import Entity "./entity/theFileName.mo" should work (where theFileName is the name of a file in the entity directory. The syntax may have changed since I last did this.

Ok but there’s no way to split an import over multiple files like in go? Sorry my question was a bit muddled. I’m trying to avoid a single file with 10,000 lines of code in.

Putting a lib.mo (if you don’t want a named file) in the directory and having it re-export multiple other files’ public types etc. will work.
I don’t know if there’s a way to have it do this automatically though, you’d have to manually add them in the lib.mo.

Ok yeah I did mess around with that but may be more trouble than it’s worth. I’m just trying to keep the list of places I define each Entity to a minimum (as they currently change quite regularly.) So one big file is probably the way to go. Thanks!

1 Like

You can define a type called Entity in a file called entity.mo and then import that everywhere else.

You might prefer to call the file types.mo, put other type definitions in there, and then do import Types "./types.mo" so you can refer to them as Types.Entity and Types.OtherType

Otherwise do import Entity "./entity.mo" and refer to it as Entity.Entity

I think he’s after a way to do a single import that pulls in multiple files, so he can split his imported code for readability. (The lib.mo/naming explicitly choice is a bit of an aside to this).

These multiple files would then all be in the same namespace. Do we maybe have a way to do this?

It’s not a big deal if you can’t. I’m working on a code generator that’ll take care of generating all the entities in one big file.

1 Like