Thread overview
How to initialise array of ubytes?
Nov 29, 2014
Paul
Nov 29, 2014
bearophile
Nov 29, 2014
Paul
Nov 30, 2014
Xinok
Nov 29, 2014
Daniel Kozak
Nov 29, 2014
bearophile
Nov 29, 2014
Daniel Kozak
Nov 30, 2014
bearophile
Nov 30, 2014
Paul
Nov 29, 2014
ketmar
November 29, 2014
I'm trying to do this:

ubyte[MAPSIZE][MAPSIZE] map = 1;

but it doesn't work and I can't seem to cast the value to a ubyte (which looks rather ugly and out of place in D anyway). Is there a way to do this other than using a couple of loops?

Cheers

Paul
November 29, 2014
Paul:

> I'm trying to do this:
>
> ubyte[MAPSIZE][MAPSIZE] map = 1;
>
> but it doesn't work and I can't seem to cast the value to a ubyte (which looks rather ugly and out of place in D anyway). Is there a way to do this other than using a couple of loops?

This works:

enum MAPSIZE = 3;
void main() {
    ubyte[MAPSIZE][MAPSIZE] map2 = 1;
}


This doesn't work:

enum MAPSIZE = 3;
ubyte[MAPSIZE][MAPSIZE] map1 = ubyte(1);
void main() {}

Why isn't this working?

Bye,
bearophile
November 29, 2014
Dne Sat, 29 Nov 2014 21:10:41 +0100 Paul via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsal(a):

> I'm trying to do this:
>
> ubyte[MAPSIZE][MAPSIZE] map = 1;
>
> but it doesn't work and I can't seem to cast the value to a ubyte (which looks rather ugly and out of place in D anyway). Is there a way to do this other than using a couple of loops?
>
> Cheers
>
> Paul

http://stackoverflow.com/questions/24600796/d-set-default-value-for-a-struct-member-which-is-a-multidimensional-static-arr/24754361#24754361
-- 
Vytvořeno poštovní aplikací Opery: http://www.opera.com/mail/

November 29, 2014
On Sat, 29 Nov 2014 21:28:36 +0100
Daniel Kozak via Digitalmars-d-learn
<digitalmars-d-learn@puremagic.com> wrote:

> Dne Sat, 29 Nov 2014 21:10:41 +0100 Paul via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsal(a):
> 
> > I'm trying to do this:
> >
> > ubyte[MAPSIZE][MAPSIZE] map = 1;
> >
> > but it doesn't work and I can't seem to cast the value to a ubyte (which looks rather ugly and out of place in D anyway). Is there a way to do this other than using a couple of loops?
> >
> > Cheers
> >
> > Paul
> 
> http://stackoverflow.com/questions/24600796/d-set-default-value-for-a-struct-member-which-is-a-multidimensional-static-arr/24754361#24754361
aaaargh, make me unsee that! that's... disgusting.


November 29, 2014
Daniel Kozak:

> http://stackoverflow.com/questions/24600796/d-set-default-value-for-a-struct-member-which-is-a-multidimensional-static-arr/24754361#24754361

Do you also know why the simplest syntax doesn't work? Can't it be implemented and added to the D language?

Bye,
bearophile
November 29, 2014
On Saturday, 29 November 2014 at 20:22:40 UTC, bearophile wrote:
> This works:
>
> enum MAPSIZE = 3;
> void main() {
>     ubyte[MAPSIZE][MAPSIZE] map2 = 1;
> }
>
>
> This doesn't work:
>
> enum MAPSIZE = 3;
> ubyte[MAPSIZE][MAPSIZE] map1 = ubyte(1);
> void main() {}
>
> Why isn't this working?
>

I'm afraid I don't know. I would guess it's something to do with trying to initialise the array in the global scope but you've also changed the expression in the non-working example. I don't have access to my machine at present so I can't experiment!

November 29, 2014
On Saturday, 29 November 2014 at 20:45:34 UTC, bearophile wrote:
> Daniel Kozak:
>
>> http://stackoverflow.com/questions/24600796/d-set-default-value-for-a-struct-member-which-is-a-multidimensional-static-arr/24754361#24754361
>
> Do you also know why the simplest syntax doesn't work? Can't it be implemented and added to the D language?
>
> Bye,
> bearophile

I don't know. But this works too:

module main;
import std.stdio;

int[5][5] p;

static this() {
	p = 1;
}

void main() {
	writeln(p);
}
November 30, 2014
Daniel Kozak:

> I don't know. But this works too:

I have added an ER:
https://issues.dlang.org/show_bug.cgi?id=13799

Bye,
bearophile
November 30, 2014
On Sunday, 30 November 2014 at 11:13:56 UTC, bearophile wrote:
> Daniel Kozak:
>
>> I don't know. But this works too:
>
> I have added an ER:
> https://issues.dlang.org/show_bug.cgi?id=13799
>
> Bye,
> bearophile

Glad to hear it's not just me being dense for a change :D
November 30, 2014
On Saturday, 29 November 2014 at 20:47:09 UTC, Paul wrote:
> On Saturday, 29 November 2014 at 20:22:40 UTC, bearophile wrote:
>> This works:
>>
>> enum MAPSIZE = 3;
>> void main() {
>>    ubyte[MAPSIZE][MAPSIZE] map2 = 1;
>> }
>>
>>
>> This doesn't work:
>>
>> enum MAPSIZE = 3;
>> ubyte[MAPSIZE][MAPSIZE] map1 = ubyte(1);
>> void main() {}
>>
>> Why isn't this working?
>>
>
> I'm afraid I don't know. I would guess it's something to do with trying to initialise the array in the global scope but you've also changed the expression in the non-working example. I don't have access to my machine at present so I can't experiment!

More generally, it's because one is static (as in global / thread-local) and the other is not. Take the working example, put "static" in front of the declaration, and you'll get the same error.

There are different rules regarding the initialization of static and non-static variables. My guess, they're implemented as separate features in the compiler, so when vector operations were introduced, nobody thought to implement this feature as a way to initialize static arrays as well.