Re: Global const variables

2014-10-21 Thread Minas Mina via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 08:02:52 UTC, bearophile wrote: Currently this code gets rejected: const int[] a = [1]; void main() pure { auto y = a[0]; } test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' test2.d(3,14): Error: pure function 'D main'

Re: Global const variables

2014-10-21 Thread bearophile via Digitalmars-d-learn
Minas Mina: Aren't pure functions supposed to return the same result every time? If yes, it is correct to not accept it. But how can main() not be pure? Or, how can't the 'a' array be immutable? Bye, bearophile

Re: Global const variables

2014-10-21 Thread safety0ff via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 08:25:07 UTC, bearophile wrote: Minas Mina: Aren't pure functions supposed to return the same result every time? If yes, it is correct to not accept it. But how can main() not be pure? Or, how can't the 'a' array be immutable? Bye, bearophile There can

Re: Global const variables

2014-10-21 Thread Szymon Gatner via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 08:48:09 UTC, safety0ff wrote: On Tuesday, 21 October 2014 at 08:25:07 UTC, bearophile wrote: Minas Mina: Aren't pure functions supposed to return the same result every time? If yes, it is correct to not accept it. But how can main() not be pure? Or, how can't

Re: Global const variables

2014-10-21 Thread bearophile via Digitalmars-d-learn
Szymon Gatner: const int[] a; int[] b; static this() { b = [1]; a = b; } Ant this code works? What is the point of const then if you can assign it to mutable slice? It works, and I think it should work. Inside the (module) constructor the const state is handled differently. Thank

Re: Global const variables

2014-10-21 Thread Solomon E via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 08:48:09 UTC, safety0ff wrote: On Tuesday, 21 October 2014 at 08:25:07 UTC, bearophile wrote: Minas Mina: Aren't pure functions supposed to return the same result every time? If yes, it is correct to not accept it. But how can main() not be pure? Or, how can't

Re: Global const variables

2014-10-21 Thread anonymous via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 12:08:35 UTC, Solomon E wrote: On Tuesday, 21 October 2014 at 08:48:09 UTC, safety0ff wrote: const int[] a; int[] b; static this() { b = [1]; a = b; } `a` isn't a reference to `b`. `a` is assigned by value and has its own storage. `a` is indeed a copy

Re: Global const variables

2014-10-21 Thread Solomon E via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 12:30:30 UTC, anonymous wrote: On Tuesday, 21 October 2014 at 12:08:35 UTC, Solomon E wrote: On Tuesday, 21 October 2014 at 08:48:09 UTC, safety0ff wrote: const int[] a; int[] b; static this() { b = [1]; a = b; } `a` isn't a reference to `b`. `a` is

Re: Global const variables

2014-10-21 Thread ketmar via Digitalmars-d-learn
On Tue, 21 Oct 2014 13:43:29 + Solomon E via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: `b[0] = 8;` or `b[] = 8;` changes a. Printing the values for a and b shows they're different pointers, but (a is b) returns true. So I still have more to learn about how it does

Re: Global const variables

2014-10-21 Thread ketmar via Digitalmars-d-learn
On Tue, 21 Oct 2014 17:25:09 +0300 ketmar via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Tue, 21 Oct 2014 13:43:29 + Solomon E via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: `b[0] = 8;` or `b[] = 8;` changes a. Printing the values for a and b

Re: Global const variables

2014-10-21 Thread MachineCode via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 08:02:52 UTC, bearophile wrote: Currently this code gets rejected: const int[] a = [1]; void main() pure { auto y = a[0]; } test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' test2.d(3,14): Error: pure function 'D main'

Re: Global const variables

2014-10-21 Thread Solomon E via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 14:25:20 UTC, ketmar via Digitalmars-d-learn wrote: On Tue, 21 Oct 2014 13:43:29 + Solomon E via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: `b[0] = 8;` or `b[] = 8;` changes a. Printing the values for a and b shows they're different

Re: Global const variables

2014-10-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, October 21, 2014 08:02:50 bearophile via Digitalmars-d-learn wrote: Currently this code gets rejected: const int[] a = [1]; void main() pure { auto y = a[0]; } test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' test2.d(3,14): Error: pure

Re: Global const variables

2014-10-21 Thread ketmar via Digitalmars-d-learn
On Tue, 21 Oct 2014 16:47:04 + Solomon E via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: That's unsafe because the implementation might change, and pointer arithmetic is unsafe in general. sure, ponter casting is implementation-dependend. but .ptr is guaranteed to work as

Re: Global const variables

2014-10-21 Thread Solomon E via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 15:51:27 UTC, MachineCode wrote: ... ... pure functions are also supposed to don't use global variables at all, according to functional programming paradigm Pure functions are immutables (constants but not const in the D or C++ senses) and can use other

Re: Global const variables

2014-10-21 Thread Meta via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 16:56:06 UTC, Solomon E wrote: On Tuesday, 21 October 2014 at 15:51:27 UTC, MachineCode wrote: ... ... pure functions are also supposed to don't use global variables at all, according to functional programming paradigm Pure functions are immutables (constants

Re: Global const variables

2014-10-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 15:51:27 UTC, MachineCode wrote: pure functions are also supposed to don't use global variables at all, according to functional programming paradigm The functional programming paradigm is kind of irrelevant to D's pure, which should really be something more like

Re: Global const variables

2014-10-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, 21 October 2014 at 17:00:49 UTC, Meta wrote: There is no such thing as global scope in D. While that's technically true (and very good for avoiding symbol conflicts), modules at the module level are still typically referred to as global variables.