Jump to page: 1 2 3
Thread overview
Assigning global and static associative arrays
Aug 31, 2012
ixid
Aug 31, 2012
Mike Parker
Aug 31, 2012
Mike Parker
Aug 31, 2012
Jonathan M Davis
Aug 31, 2012
ixid
Aug 31, 2012
Philippe Sigaud
Aug 31, 2012
Jonathan M Davis
Aug 31, 2012
ixid
Aug 31, 2012
ixid
Aug 31, 2012
Jonathan M Davis
Sep 01, 2012
Philippe Sigaud
Aug 31, 2012
Philippe Sigaud
Aug 31, 2012
ixid
Aug 31, 2012
Philippe Sigaud
Sep 01, 2012
Jonathan M Davis
Sep 01, 2012
Philippe Sigaud
Sep 01, 2012
Jonathan M Davis
Sep 01, 2012
ixid
Sep 01, 2012
Jonathan M Davis
Sep 02, 2012
monarch_dodra
Sep 02, 2012
Timon Gehr
Sep 02, 2012
monarch_dodra
Sep 02, 2012
Timon Gehr
Sep 02, 2012
Ali Çehreli
Sep 02, 2012
Philippe Sigaud
Sep 03, 2012
Ali Çehreli
August 31, 2012
Why does this not work:

int[string] dayNumbers =
        [ "Monday"   : 0, "Tuesday" : 1, "Wednesday" : 2,
          "Thursday" : 3, "Friday"  : 4, "Saturday"  : 5,
          "Sunday"   : 6 ];

void main() {
    //Stuff
}

With the error 'non-constant expression'? This error also seems to prevent me making static associative arrays in functions elegantly.
August 31, 2012
On 8/31/2012 11:38 PM, ixid wrote:
> Why does this not work:
>
> int[string] dayNumbers =
>          [ "Monday"   : 0, "Tuesday" : 1, "Wednesday" : 2,
>            "Thursday" : 3, "Friday"  : 4, "Saturday"  : 5,
>            "Sunday"   : 6 ];
>
> void main() {
>      //Stuff
> }
>
> With the error 'non-constant expression'? This error also seems to
> prevent me making static associative arrays in functions elegantly.

Non-const variables cannot be initialized at module-scope. If you don't need to modify the aa, declaring it immutable should allow the initialization to work. Otherwise, you can initialize it using a static module constructor.

int[string] dayNumbers;

static this()
{
   dayNumbers = ...;
}

void main() {...}
August 31, 2012
On 9/1/2012 1:31 AM, Mike Parker wrote:
> On 8/31/2012 11:38 PM, ixid wrote:
>> Why does this not work:
>>
>> int[string] dayNumbers =
>>          [ "Monday"   : 0, "Tuesday" : 1, "Wednesday" : 2,
>>            "Thursday" : 3, "Friday"  : 4, "Saturday"  : 5,
>>            "Sunday"   : 6 ];
>>
>> void main() {
>>      //Stuff
>> }
>>
>> With the error 'non-constant expression'? This error also seems to
>> prevent me making static associative arrays in functions elegantly.
>
> Non-const variables cannot be initialized at module-scope. If you don't
> need to modify the aa, declaring it immutable should allow the
> initialization to work. Otherwise, you can initialize it using a static
> module constructor.
>
> int[string] dayNumbers;
>
> static this()
> {
>     dayNumbers = ...;
> }
>
> void main() {...}

Nevermind. I spoke too soon. It's the literal ["Monday" : 0, ...] that's not constant. You'll have to use the module constructor.

August 31, 2012
On Friday, August 31, 2012 16:38:13 ixid wrote:
> Why does this not work:
> 
> int[string] dayNumbers =
> [ "Monday" : 0, "Tuesday" : 1, "Wednesday" : 2,
> "Thursday" : 3, "Friday" : 4, "Saturday" : 5,
> "Sunday" : 6 ];
> 
> void main() {
> //Stuff
> }
> 
> With the error 'non-constant expression'? This error also seems to prevent me making static associative arrays in functions elegantly.

Because they can't be declared at runtime like that. For that to work, they'd have to be constructable at compile time and then have the structure persist to runtime, and with all of the pointers and whatnot in an AA, that's far from trivial. The same occurs with classes. While you can use some of them in CTFE, you can't use them to directly initialize any variables whose values need to be known at compile time and persist until runtime.

You have to use static constructor to initialize the variable at runtime.

static this()
{
 //initialization code here...
}

- Jonathan M Davis
August 31, 2012
Thank you, that certainly makes sense.
August 31, 2012
On Fri, Aug 31, 2012 at 7:04 PM, ixid <nuaccount@gmail.com> wrote:
> Thank you, that certainly makes sense.

If you're certain you won't need to modify it, you can make it a compile-time constant:

enum int[string] dayNumbers =
        [ "Monday"   : 0, "Tuesday" : 1, "Wednesday" : 2,
          "Thursday" : 3, "Friday"  : 4, "Saturday"  : 5,
          "Sunday"   : 6 ];

void main() {
    //Stuff
}
August 31, 2012
On Friday, August 31, 2012 20:24:27 Philippe Sigaud wrote:
> On Fri, Aug 31, 2012 at 7:04 PM, ixid <nuaccount@gmail.com> wrote:
> > Thank you, that certainly makes sense.
> 
> If you're certain you won't need to modify it, you can make it a compile-time constant:
> 
> enum int[string] dayNumbers =
> [ "Monday" : 0, "Tuesday" : 1, "Wednesday" : 2,
> "Thursday" : 3, "Friday" : 4, "Saturday" : 5,
> "Sunday" : 6 ];
> 
> void main() {
> //Stuff
> }

Except that that allocates a new AA every time that you use dayNumbers. So, that's probably a bad idea.

- Jonathan M Davis
August 31, 2012
Philippe suggested enum allowing this:

enum dayNumbers = [ "Monday" : 0, "Tuesday" : 1, "Wednesday" : 2,
 "Thursday" : 3, "Friday" : 4, "Saturday" : 5,
 "Sunday" : 6 ];

Why does this seem to avoid pointer issues? Is it creating a compile-time associated array or run-time?
August 31, 2012
On Fri, Aug 31, 2012 at 9:56 PM, Jonathan M Davis <jmdavisProg@gmx.com> wrote:

> Except that that allocates a new AA every time that you use dayNumbers. So, that's probably a bad idea.

Oh! I keep forgetting that enums are replaced by their values. Then I think I know where some problem I had came from.

Damn, just tested using a static this and my code runs 40% faster!

Benchmarking time, thanks Jonathan.
August 31, 2012
On Fri, Aug 31, 2012 at 10:34 PM, ixid <nuaccount@gmail.com> wrote:
> Philippe suggested enum allowing this:
>
> enum dayNumbers = [ "Monday" : 0, "Tuesday" : 1, "Wednesday" : 2,
>
>  "Thursday" : 3, "Friday" : 4, "Saturday" : 5,
>  "Sunday" : 6 ];
>
> Why does this seem to avoid pointer issues? Is it creating a compile-time associated array or run-time?

It defines an AA literal that's used directly in lieu of dayNumbers every time dayNumbers appears in your code, I think.
« First   ‹ Prev
1 2 3