Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static constant as Array size #70

Open
samphunter opened this issue Jun 19, 2015 · 16 comments
Open

static constant as Array size #70

samphunter opened this issue Jun 19, 2015 · 16 comments

Comments

@samphunter
Copy link

const Size = 5;
new data[Size]; // Works fine

static const Size = 5;
new data[Size]; // error 008: must be a constant expression; assumed zero

@Zeex Zeex closed this as completed in 39828f2 Jun 19, 2015
@Zeex Zeex reopened this Jun 27, 2015
@YashasSamaga
Copy link
Member

YashasSamaga commented Dec 26, 2016

The compiler treats static constants as "variables". Since, it is considered to be a variable instead of a constant, you are getting that error.

Static constants are declared as variables.
https://github.com/Zeex/pawn/blob/master/source/compiler/sc1.c#L4976
https://github.com/Zeex/pawn/blob/master/source/compiler/sc1.c#L2160

non-static constants are declared as constants.
https://github.com/Zeex/pawn/blob/master/source/compiler/sc1.c#L5050

findconst does not consider static constants as constants which is causing the problem.

@Daniel-Cortez
Copy link
Contributor

Daniel-Cortez commented Jan 5, 2018

Thanks to @VVWVV's work, starting from release 3.10.2 it's possible to use static enumerations to define constants with a scope limited to a single file.

static enum { ARRAY_SIZE = 20 };
new a[ARRAY_SIZE];

@Southclaws, you probably want to close this.

@YashasSamaga
Copy link
Member

@Southclaws @Daniel-Cortez In my opinion, the issue is still not resolved. As const non-static variables can be used as array length, static const should also be allowed. Yes, there is another way out but the ability to use static const as an array argument isn't resolved.

As a matter of fact, we still can't create local static constants. Using a static enum would pollute the global namespace of the file when you just want the constant to remain visible just within a function or a block.

@Southclaws
Copy link
Collaborator

Ah I misread the post from Daniel and thought static const was resolved.

@Daniel-Cortez
Copy link
Contributor

Daniel-Cortez commented Jan 6, 2018

@YashasSamaga, so how do you imagine "fixing" this? Treat all identifiers defined with static const as compile-time constants instead of read-only variables? Such change can probably break existing code.

@Southclaws
Copy link
Collaborator

If const works, surely static const isn't much different? The only difference is the access constraint to the section/file it's in.

@YashasSamaga
Copy link
Member

YashasSamaga commented Jan 6, 2018

@Daniel-Cortez Hmm, I get your point. I think my own code will get screwed if static const is treated as compile-time constants because I access it through assembly.

Why not simply use the value of the static const variable as a compile-time constant and leave the variable as it is?

EDIT: I see. The fix is pretty complicated: will have to make a new entry or use an existing entry in the symbol structure to store the initialization value for local static constants.

@Southclaws
Copy link
Collaborator

If the fix is complicated, is this really worth the work? Symbolising constants with #define is good enough, right?

@YashasSamaga
Copy link
Member

Not sure if there is a better solution but the solution I mentioned is awfully bad as it involves adding a new member to the symbol structure just for the sake of static const variables so that they can be used to set the size of an array.

@Daniel-Cortez
Copy link
Contributor

Daniel-Cortez commented Jan 6, 2018

Also it would be pretty strange to the same identifier acting as both a variable and a compile-time constant at the same time, IMHO.

@samphunter
Copy link
Author

samphunter commented Jan 6, 2018

@Southclaws Well you would have to use #undef at the end of the file to simulate static. But in a way, yes. You can use the enum trick now, as was mentioned before. Fixing this at this point would just allow for slightly nicer code and would make constants more consistent.

It is up to you to decide, whether making constants behave consistently (currently, const behaves differently from static const, which it probably should not) is worth the effort or not.

I mostly wanted this fixed, because static enums were not a thing back then.

@Y-Less
Copy link
Member

Y-Less commented Jan 6, 2018

Also it would be pretty strange to the same identifier acting as both a variable and a compile-time constant at the same time, IMHO.

Global consts already do that, as they are compile-time constant. IMHO anything static should work like the global equivalent, just with reduced scope - be that file level or function level; if global consts can declare array sizes (TIL), then IMHO so should static consts. "It breaks #emit code" is not really a great argument against things, and I say that as someone aware that it might break their own code. Writing #emit is already beyond the scope of PAWN, and is generally used to bypass language constraints.

@YashasSamaga
Copy link
Member

YashasSamaga commented Jun 24, 2018

strings with just const cannot be declared (because plain const makes the symbol a compile time constant but strings cannot be compile time constants) but static const can declare an array

If we make static const behave like compile-time constants, it would break code.

@stale
Copy link

stale bot commented Oct 26, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale label Oct 26, 2018
@Daniel-Cortez
Copy link
Contributor

Daniel-Cortez commented Dec 23, 2020

I know this issue is kind of old, but since it's still not fully resolved, what if we introduce a static const const combination of class specifiers, which would allow us to tell the compiler we want an actual constant and not an immutable variable? Even though we have static enumerations now, they're still not the same thing, as their syntax is completely different from single constant declarations.

@stale
Copy link

stale bot commented Jun 22, 2021

This issue has been automatically marked as stale because it has not had recent activity.

@stale stale bot added the state: stale label Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants