Class naming rules?

Monkey Forums/Monkey Programming/Class naming rules?

ondesic(Posted 2012) [#1]
I have a myGame.monkey file which has the main function in it. I also have a few class files in the same folder making the folder contain.

myGame.monkey
cards.monkey
player.monkey

Now here is my question:
In the myGame file I import the two class files

Import cards
Import player

however, monkey is not recognizing them.

The problem is in the cards.monkey file, the class is name is the same as the file:

Class cards
End

If I change the class name, it works. Why can't I name the file the same as the class inside it?

Thanks


ziggy(Posted 2012) [#2]
Becouse file names are also modules names, but you can avoid module names when you refer to a class, to make things easier.
Imagine this:
'File myClass.monkey
Class myClass
   Method Draw()
      ...
      ...
   End
End

Function Main()
   Local b:=New myClass.myClass  'This is a reference to the class myClass inside the module myClass.
   Local c:=New myClass 'This is a refence to the module (file) myClass, wich is something you cannot instantiate. So this is a syntax error.
End


Monkey naming convention is to use lowercase for files, and CammelCase for classes.


ondesic(Posted 2012) [#3]
Makes sense. Thanks!


Kauffy(Posted 2012) [#4]
Please, please, in the name of all that is holy, unless you're absolutely positive nobody will ever see your code, follow the Monkey convention in the manual :) :


The standard Monkey modules use a simple naming convention:

•All-caps case (eg: 'ALLCAPS' ): Constants.

•Pascal case (eg: 'PascalCase' ): Globals, functions, classes, methods, properties.

•Camel case (eg: 'camelCase' ): Fields, locals and function parameters.

You are of course free to use your own convention, but for the sake of consistency it is recommended that this convention be used for the public interface of any modules you create intended for use by the Monkey community.



I also put filenames in the camelCase category-- this way, they don't clash with Classes.

[EDIT:] To answer your question, also, the reason you "can't" refer to a file and a class with the same identifier (same name/same case) is that each one represents a namespace. So you can actually refer to a variable defined inside a file just as you would a class member.

So if you had a global called 'count' defined in a file called garbage.monkey and then a class called garbage, with a member called 'count', then 'garbage.count' could refer to either.

You would use this to differentiate between variables declared in different files when there is a conflict-- like if you imported a module that declared a global with the same name as one you want to use. You can differentiate between them with modulename.Global vs. myfile.Global, etc.