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

Setting array sizes from constants throws #11

Open
gmarty opened this issue Jul 26, 2012 · 3 comments
Open

Setting array sizes from constants throws #11

gmarty opened this issue Jul 26, 2012 · 3 comments

Comments

@gmarty
Copy link

gmarty commented Jul 26, 2012

Defining an array size from a constant or variable throws a messageFormat is undefined error:

const SIZE = 100;

let int arr[SIZE];
struct ArrayStruct {
  int arr[SIZE];
};

Both declarations throws the error.

Arrays allocated on the heap are not affected though.

@mbebenita
Copy link
Owner

This is a bit tricky, and what you really need is a macro pre-processor, which we don't have. SIZE here is an identifier and is not substituted with 100. Imagine you had const SIZE = getSize(), you can't figure out statically how much space to allocate on the stack. Maybe we can do something to track const declarations that can be evaluated at compile time.

@luiscubal
Copy link

C++ has constexpr. Perhaps LLJS could implement a similar mechanism?
Alternatively, C99 has variable length arrays. So it should be possible.

@rinon
Copy link
Collaborator

rinon commented Jul 27, 2012

Sorry, it shouldn't have thrown. I was missing an error message. Trying to use a const (or any other expression) now at least prints a nice error.

As far as implementing const sizes, yes, it might be possible, but at the moment the structure of the compiler and when array size calculation happens is not conducive to checking expressions to make sure they are really const literals (we don't have scoping or variable information at the point where size calculation happens).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants