Namespaces

Monkey Forums/Monkey Programming/Namespaces

Xaron(Posted 2012) [#1]
Hi all,

I just think about how to realize namespaces in Monkey. Any idea?

A class inside a class doesn't work so I thought about using some kind of a dummy global variable...


Shinkiro1(Posted 2012) [#2]
Using the module name in front of the class name should do it.
Say you have a file/module called audio.monkey and in it a class named Music.

You can then access it like this from anywhere else.
audio.Music.Method()



Xaron(Posted 2012) [#3]
Well yes, thanks Shinkiro! Should have thought about that module stuff! :)


Xaron(Posted 2012) [#4]
Hmm... What if I want to create one file for every class (like I usually do in C++)? Let's say I have the namespace "utils" and the class "Fps" which just computes the frames per second.

I'd like to have the class "Fps" in a file called fps.monkey within a folder utils...

So with that module concept I would have a file "utils.monkey" containing all classes in that "namespace"?


ziggy(Posted 2012) [#5]
No, there are no namespaces in monkey, but you can use the module name to avoid clashes, so you can call the Fps class fps.Fps
If you can't avoid it this way, you can also use the Alias keyword.


Tibit(Posted 2012) [#6]
You can have a folder util with lots of monkey files in. And you can import utils and you import everything in that folder by having a untils.monkey file with all imports in that folder. This is the way I have it in utoopia.

It works with any level of hierarcies (not sure if this is what you are after). But it means that I can either import utoopia OR import utoopia.misc OR import utoopia.misc.fpscounter as an example.

Especially handy since Monkey only translates what I use, so importing everything results in no overhead at all.