Re: Bug in 1.5a ?
- Posted by Robert Craig <rds at EMAIL.MSN.COM> Feb 07, 1998
- 789 views
Apparently a lot of people have been severely confused by the "global" keyword in Euphoria. I will improve the description in REFMAN.DOC. See also TROUBLE.DOC if you have found that routines / vars declared in an include file are reported as "not declared" when you try to use them in another file. The situation is this: If you declare a constant, variable or routine and you do not put the keyword "global" in front of the declaration, that variable or routine declaration will become invisible at the end of its file. It will not be accessible from any other file. e.g. -- file x.e global procedure foo() end procedure Without the word "global", foo() would be unknown outside of x.e. It would only be known inside x.e from the point where it is declared to the end of x.e. Only code in x.e after the declaration of foo() would be able to call foo(). With the word "global", foo() will be visible from the point it is declared right through the rest of the program, in all other files. Many people wrongly assume that "global" is the default, and are puzzled when a declared routine or variable is said to be "not declared" when called from another file. I guess that's because BASIC doesn't have this concept, and in C it's inverted - global is the default situation and you need the optional keyword "static" to *prevent* a routine or variable from being known outside of its file. The guy who invented C++ (Stroustrup) has stated that C has it wrong - it should have been defined the other way (Euphoria's way), i.e. default to "not global". Why bother with all this? Consider that you make a .e include file for other people to use. Suppose your include file has 20 routines and 30 variables, but the user of your file should only use 2 of the routines and none of the variables. Without the global/not-global feature, you would have to add 20+30 = 50 names to his program, names that he is not allowed to use. This might cause naming conflicts for him and he would have to change some of his names or some of your names. Furthermore, the global keyword would help to remind him of which routines/variables he can legitimately use, and which are internal to your code. When you later modify your .e file, it will help you to know that some routine or variable is not global, therefore you will not break other people's code by changing/removing it. If global were the default, people would be lazy and there would be many internal routines/vars exposed as global that needn't be. With "not global" as the default, the tendency is that only the routines/vars that actually need to be global will be declared that way. Regards, Rob Craig Rapid Deployment Software