`static` symbol needs to be `immutable` for compile-time access?

2016-01-22 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. This is a minimal abstraction of a part of my program: int func(string s) { static int [] i = [5, 6, 7]; return i[2]; } template temp(string s) { enum temp = func(s); } void main() { static assert(temp!"str" == 7); } With the above code I get: (4): Error: static variable i cannot

Re: `static` symbol needs to be `immutable` for compile-time access?

2016-01-22 Thread Kagamin via Digitalmars-d-learn
Why do you declare mutable constants?

Re: `static` symbol needs to be `immutable` for compile-time access?

2016-01-22 Thread Mike Parker via Digitalmars-d-learn
On Friday, 22 January 2016 at 09:56:27 UTC, Shriramana Sharma wrote: In C/C++ the `static` here is used to avoid the array being created every time the function is entered; in D too it does the same thing, no? So if I have an array of constants in a function that I need to be accessible to a

Re: `static` symbol needs to be `immutable` for compile-time access?

2016-01-22 Thread Marc Schütz via Digitalmars-d-learn
On Friday, 22 January 2016 at 10:15:19 UTC, Mike Parker wrote: A static variable is still a runtime variable. It's effectively the same as declaring a variable outside of the function scope at module scope, except that it's visible only in the current scope and the function name gets mangled

Re: `static` symbol needs to be `immutable` for compile-time access?

2016-01-22 Thread anonymous via Digitalmars-d-learn
On 22.01.2016 10:56, Shriramana Sharma wrote: Do all values which need to be readable at compile time need to be declared `immutable`? Yes, `static immutable` or `enum`. In C/C++ the `static` here is used to avoid the array being created every time the function is entered; in D too it does

Re: `static` symbol needs to be `immutable` for compile-time access?

2016-01-22 Thread Shriramana Sharma via Digitalmars-d-learn
Thanks to all who replied. anonymous wrote: >> Do all values which need to >> be readable at compile time need to be declared `immutable`? > > Yes, `static immutable` or `enum`. It would seem that in the case of arrays, the former is preferable to the latter, as per the para above this header:

Re: `static` symbol needs to be `immutable` for compile-time access?

2016-01-22 Thread anonymous via Digitalmars-d-learn
On 22.01.2016 15:33, Shriramana Sharma wrote: It would seem that in the case of arrays, the former is preferable to the latter, as per the para above this header: http://ddili.org/ders/d.en/const_and_immutable.html#ix_const_and_immutable.variable, %20immutable The link doesn't work for me,