main method must appear at top level

DaMaster0

Member
main method must appear at top level

I'm getting this a lot recently. What's causing it? it's seemingly random and it's invading all of my scripts!
 

Veracity

Developer
Staff member
You can have functions declared inside of other functions, in which case they can only be called from within that function.

The exception is the function named "main". That one cannot appear inside another function. If you try to do that, you get the error message you list.
 

Veracity

Developer
Staff member
Can this also happen if you are importing scripts that have their own mains?

No. main() is ignored in imported files and recognized only for the specific script you called.

Personally, I don't use main() functions unless I need to prompt the user for arguments. If not, I just have commands at toplevel.

When I mentioned this a couple years ago, I was publicly informed that I was wrong, and privately informed that I was clearly an inferior programmer because I did that, so, your milage may vary. :)
 

Catch-22

Active member
When I mentioned this a couple years ago, I was publicly informed that I was wrong, and privately informed that I was clearly an inferior programmer because I did that, so, your milage may vary. :)

Yeah and don't even get them started on the inferiority of Java as a programming language ;)
 

Rinn

Developer
Personally I like having a main() as it's what most people would look for first to find the entry point to the script, however I don't think main() should do anything other then call a separate function and pass through all the necessary parameters so that your script is easier to work with when importing.

I wouldn't call someone an inferior programmer unless they were doing something idiotic like creating a pointer to a bool to store flags. True story, and in fact I think this guy had an stl vector of bool pointers he was using as flags it was pretty ridiculous.
 

zarqon

Well-known member
Actually, you get this error if you import a script like this, even at top level:

Code:
if (condition) {
   import <script>;
}

The import command has to be top-level to avoid this error, which means scripts with main() functions cannot be conditionally imported.
 
Top