Can Import operate backwards?

Monkey Forums/Monkey Programming/Can Import operate backwards?

Shagwana(Posted 2015) [#1]
Given the following two files...

File#1 = home\example\program.monkey
File#2 = home\lib\test.monkey

Is it possible for 'program.monkey' to import 'test.monkey'?

If so, how!?


MikeHart(Posted 2015) [#2]
Look at my framework. fantomEngine.monkey imports all the modules. The modules import fantomEngine.


Shagwana(Posted 2015) [#3]
Had a little nose and from what I can see none of them do a 'backwards' directory structure import.

If I take one of your examples for example; I unpacked the zip and used the following example at its current location "\examples\Text\FontMachine\FontMachine.monkey". This does not perform a successful import of the "fantomEngine" because it does not look backwards for it. It exists a few directorys lower. If I try to run the example, I get an error of "Error : Type 'App' not found" (known bug - does not flag missing file as error).

What I was hoping to be able to do was import ... File#2 into File#1 (as per my above example)


Just wanted to add that I already use a global import file, but what I really want to do is compartmentalize the code into different folders at the same level.


Why0Why(Posted 2015) [#4]
In normal OS operations the .. operator takes you up a directory so it should be like the following:

..\lib\test.monkey

I would expect that to work.


Nobuyuki(Posted 2015) [#5]
Import keyword has a folder precedence in the linker. The folder that the document calling Import lives in is checked first. Then, the entry point of the project. Therefore, you can link to modules in other folders by typing the path relative to the entry point.


ImmutableOctet(SKNG)(Posted 2015) [#6]
As Nobuyuki said, it's relative. That being said, your global module folders are used as a fall-back. Just don't use quotes when using 'Import' unless you're importing an external file, rather than a normal module.


Shagwana(Posted 2015) [#7]
1st off...
Import ..\lib\test.monkey <- does not work, this is not valid

2nd...
The entry point in my example above is the folder \example\ so the relative path is ..\lib\test.monkey (as others have already suggested above)

To me it looks like its not possible to use Imports with relative paths as detailed by my problem above.


ziggy(Posted 2015) [#8]
Imports are:
1.- Relative to the file where the import is found --> That implies same folder or subfolder
2.- Relative to the program entry point --> That can allow files to be on any parent folder
3.- Relative to the modules folder(s) --> This allows any module to be placed on a specific folder or subfolder into the "modules" structure

That gives the whole Monkey modules approach a very nice modular nature. Disallowing parent folder imports would break this modular design if you ask me.

Now, if your structure is a library with samples in a subfolder, the usual approach is to make the library a module, and let the samples import the module. in this scenario, it would not matter if the library is on a parent folder, or anywhere else.


Shagwana(Posted 2015) [#9]
My solution was to change the entry point. This has a program called main.monkey that imports the correct file in the \example\ folder (sort of a little 'stub'). Added benefit of this is now I only need one .data folder for all my examples!.