The V docs say that you can create a module in a folder such as
my_module
file1.v
file2.v
file1.v
module my_module
// code...
file2.v
module my_module
// code...
module my_module is same as the folder namepub if we want to access file1.v's functions in file2.v?As of today, if I want to build a module using v build module, I have to do it like this
v build module ~/path/to/module/my_module
Even if I am in the parent module I can't do:
v build module my_module
OR
v build module ./my_module
After building the module my_module, I go into some other directory and create a hello.v file. In that file I import my_module but the compiler gives error:
cannot import module "my_module" (not found)
Although I can see that the module built correctly and it is present in ~/.vmodules
If I have a folder my_folder and inside it I have my module's code. Now I want to create a small demo so, I create a folder demo inside my_folder and create a demo.v in it. In the file demo.v I want to call functions from the parent folder. How can I do that?
Thank you for reading this. I would highly appreciate if someone or the author of the language can answer some of these questions as the documentation is scarce right now.
@Waqar144 Hi!
my_module, the first module access is my_module.v. This "main" file should have an init function (not public, that initializes the module, if necessary) and it's your interface for external code. In your case, for example, you can call a function declared in file1.v or file2.v from my_module.v but not in the other verse.file2.v or file1.v can be accessed from my_module.v without pub.v build module ~/VProjects/modules/my_modulepub may be enough (not sure)Advice: declare your pub interfaces, functions or types in the main file (with the same name as module) and use others files for modularize logic and types
@LorenzoPirro
Advice: declare yourpubinterfaces, functions or types in the main file (with the same name as module) and use others files for modularize logic and types
Well I did this with my internal little project right the other way. Is there any reason you suggest this? I'm totally aware that I'm not totally aware of everything. ;-)
I did separate consts, enums and structs in separate files. I have a main file (mymodule.v) and some separate files for input, transform and export.
Seems a bit like overkill at first but if your projec grows it'll become handy.
@LorenzoPirro
Relative paths work fine, in my case
v build module ~/VProjects/modules/my_module
How about doing v build module my_module in ~/VProjects/modules/? Furthermore, whatever you pass as the directory, v creates the same exact directory structure in the ~/.vmodules folder. It should instead only create the files for the module.
I haven't tried but pub may be enough (not sure)
A sub-folder cannot access the main module's code. For example, running demo.v in this scenario results in an error:
+ my_module
- my_module.v (has a pub function "print" that prints 'hello world')
+ demo
- demo.v (this imports my_module and calls the "print" function)
The commands I ran to test this:
cd my_module/demo
v run .
How about this scenario:
+ my_modules
+ another
- another.v
+ something
- something.v
How do I run something.v which uses functions from another module/folder? Simply running v run something.v results in an error.
Hi @Waqar144
A module yourmodule is just a folder with .v files, containing module yourmodule at the top.
You can name the files in the module however you want, although the convention is to have one yourmodule/yourmodule.v file.
That allows you to organize the files in a module by topic.
You can have submodules/subfolders. They are independent modules, which are just organized in a hierarchical fashion under the namespace of the containing module. You can import them like this:
folder structure: module1/submodule2/ => import module1.submodule2
or
folder structure: module1/submodule2/ => import module1.submodule2 as sm
A module may have one fn init() { function, but it is optional. If it has, that function will be called automatically at program startup time if the program imports the module.
There is no need to call v build module for your module, unless you want to produce an .o file from your module - v is perfectly happy to use the code in your module in source form.
The lookup for modules happens in this order:
a) local ./my_module/ folder.
b) v's vlib/my_module/ folder.
c) ~/.vmodules/my_module/ folder.
So the answers to your questions are:
A1: Yes, it is necessary to have the name of the module same as the folder name. This is so that later you can import your module.
A2: The files inside a module are compiled all together as one unit, so they see each other functions without problems
A3: No. Each file in the module is equal to all other files in the module, and they all are in the same scope. You do not need to mark functions as pub. Pub is need only for functions that the module wants to make public to its importers.
A4: No. Thus said, using absolute paths helps with diagnosing problems...
A5: You can not directly. What you can do though is make a symlink:
ln -s /abs/path/to/my_folder/my_module ~/.vmodules/my_module
After that, you can import my_module from anywhere.
@thecodrr
How about this scenario:
- my_modules
- another
- another.v
- something
- something.v
How do I run something.v which uses functions from another module/folder? Simply running v run something.v results in an error.
Option A: Do ln -s /abs/path/to/your/my_modules ~/.vmodules/my_modules
After that, inside something.v:
import my_modules.another
Option B: Do cd my_modules/something; ln -s ../../my_modules my_modules
After that, inside something.v:
import my_modules.another
Related to this question: because the _v build module path_ command now tries to create a subdirectory path beneath the home-directory c:userusername this gives a bug on Windows 10:
for example:
Building module "E:.Vlang.The_Way_to_V.Chapter_11_Modules_and_Testing.mod1" (dir="E:VlangThe_Way_to_VChapter_11_Modules_and_Testingmod1")...
Generating a V header file for module E:\Vlang\The_Way_to_V\Chapter_11_Modules_and_Testing\mod1
V panic: failed to create file "C:UsersCVO.vmodulesE:VlangThe_Way_to_VChapter_11_Modules_and_Testingmod1.vh"
print_backtrace_skipping_top_frames is not implemented on this platform for now...)
Clearly the path beneath .vsmodules contains a drive letter (here E:), which is not allowed in Windows.
Most helpful comment
@thecodrr
Option A: Do
ln -s /abs/path/to/your/my_modules ~/.vmodules/my_modulesAfter that, inside something.v:
import my_modules.anotherOption B: Do
cd my_modules/something; ln -s ../../my_modules my_modulesAfter that, inside something.v:
import my_modules.another