Currently, redeclaring an enum constant that already exists produces an error, even if the definition is the same. E.g.:
typedef enum { one = 1 } myEnum;
typedef enum { one = 1 } myEnum;
This will produce a message like this:
*ERROR *filename* Line *n*: Constant 'one' already exists.
And the template or script will abort.
Of course, this is an artificial example. But I’ve encountered this when a script and template #include the same file. There’s no way that the script can know that the template has already done this typedef. And an author might like to have the definition explicitly visible in both.
Would it make more sense for this to be a warning when the definitions are identical, allowing execution of the template or script, but a fatal error if they differ?
It’s hard for a mere user like me to anticipate all the implications such a change might have. I just wanted to pose the question.
Declaring duplicate enums does generate an error and I believe it generates an error in regular C/C++ as well. The solution is usually to place your code inside a header and then at the beginning of the header have the line:
#ifndef MYHEADER_H
#define MYHEADER_H
and at the end of the header have
#endif
This ensures that the code in the header file is only declared once, even if the file is included multiple times. If you are having trouble between scripts and templates you would have to separate the code to have some headers that are included by the template and some headers that are included by the script.
If you are having trouble between scripts and templates you would have to separate the code to have some headers that are included by the template and some headers that are included by the script.
Follow-up question: I’m not aware of any way that a piece of 010 script/template language could conditionally perform a typedef based on a test for:
Running in template context or script context, or, alternatively,
Whether or not a template has already been run on the file (perhaps the same as asking whether a Template Results window currently exists.
I don’t believe there is any way for the same code to be included by both a script and a template and then do different things depending upon which one the code is being called by. We could add a constant that is only defined when being run as a script or a function that tells the status and we’ll look into this. In a script, if you want to check if a template has been run you should be able to use the ‘exists’ keyword to check if a template variable has been created.
I want the script and the template to do the same thing in that I want the typedef to work identically. (I frequently use typedef enum in include files to save typing out the same definitions in multiple contexts.)
The script uses exists() right now to determine if the template has already been run.
But the syntax error on the script’s typedef is apparently detected (by an earlier pass?) before exists() is tested.