www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to initialise array of ubytes?

reply "Paul" <paul example.com> writes:
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
Nov 29 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Nov 29 2014
parent reply "Paul" <paul example.com> writes:
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!
Nov 29 2014
parent "Xinok" <xinok live.com> writes:
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.
Nov 30 2014
prev sibling next sibling parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
-- Vytvořeno poštovní aplikací Opery: http://www.opera.com/mail/
Nov 29 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Daniel Kozak:


Do you also know why the simplest syntax doesn't work? Can't it be implemented and added to the D language? Bye, bearophile
Nov 29 2014
parent reply "Daniel Kozak" <kozzi11 gmail.com> writes:
On Saturday, 29 November 2014 at 20:45:34 UTC, bearophile wrote:
 Daniel Kozak:


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); }
Nov 29 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Nov 30 2014
parent "Paul" <paul example.com> writes:
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
Nov 30 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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 =20
 <digitalmars-d-learn puremagic.com> napsal(a):
=20
 I'm trying to do this:

 ubyte[MAPSIZE][MAPSIZE] map =3D 1;

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

 Cheers

 Paul
=20 http://stackoverflow.com/questions/24600796/d-set-default-value-for-a-str=
aaaargh, make me unsee that! that's... disgusting.
Nov 29 2014