www.digitalmars.com         C & C++   DMDScript  

D.gnu - Is this right?

reply Regan Heath <regan netwin.co.nz> writes:
Hi,

Ok basically I have a big static array, into which I want 4 static slices.

I thought this would work...

static char[6] a = "abcdef";
static char[3] p = a[0..3];
static char[3] q = a[3..6];

but, the slices are char[] and you cannot assign a char[] to a char[3] 
(even if they are the same length). So I tried...

static char[6] a = "abcdef";
static char[3] p = cast(char[3])a[0..3];
static char[3] q = cast(char[3])a[3..6];

and now it does not like it because cast(char[3])a[0..3] is a non constant 
expression.

So, is there any way to define a bunch of static slices into a static 
array?


I did get this to compile...

static char[6] a = "abcdef";
char[3] p;
char[3] q;

static this() {
	p[] = a[0..3];
	q[] = a[3..6];
}

int main(char[][] args) {
	return 0;
}

but, I am not 100% certain I know what it's doing exactly? is it copying? 
or just taking a static slice...

Regan.

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 08 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
Whoops, I posted this is so totally the wrong group. My appologies. And 
now I'm cross-posting.. can the kind people in digitalmars.D please read 
this. :)

On Wed, 09 Jun 2004 10:34:53 +1200, Regan Heath <regan netwin.co.nz> wrote:

 Hi,

 Ok basically I have a big static array, into which I want 4 static 
 slices.

 I thought this would work...

 static char[6] a = "abcdef";
 static char[3] p = a[0..3];
 static char[3] q = a[3..6];

 but, the slices are char[] and you cannot assign a char[] to a char[3] 
 (even if they are the same length). So I tried...

 static char[6] a = "abcdef";
 static char[3] p = cast(char[3])a[0..3];
 static char[3] q = cast(char[3])a[3..6];

 and now it does not like it because cast(char[3])a[0..3] is a non 
 constant expression.

 So, is there any way to define a bunch of static slices into a static 
 array?


 I did get this to compile...

 static char[6] a = "abcdef";
 char[3] p;
 char[3] q;

 static this() {
 	p[] = a[0..3];
 	q[] = a[3..6];
 }

 int main(char[][] args) {
 	return 0;
 }

 but, I am not 100% certain I know what it's doing exactly? is it 
 copying? or just taking a static slice...

 Regan.

-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 08 2004
next sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <opr9arqga75a2sq9 digitalmars.com>, Regan Heath says...

Do this:

 static char[6] a = "abcdef";
 static char[] p = a[0..3];
 static char[] q = a[3..6];


Arcane Jill
Jun 08 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Tue, 8 Jun 2004 22:58:17 +0000 (UTC), Arcane Jill 
<Arcane_member pathlink.com> wrote:
 In article <opr9arqga75a2sq9 digitalmars.com>, Regan Heath says...

 Do this:

 static char[6] a = "abcdef";
 static char[] p = a[0..3];
 static char[] q = a[3..6];



It doesn't work "non-constant expression a[0..3]". Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 08 2004
parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <opr9auakwt5a2sq9 digitalmars.com>, Regan Heath says...
 Do this:

 static char[6] a = "abcdef";
 static char[] p = a[0..3];
 static char[] q = a[3..6];



It doesn't work "non-constant expression a[0..3]".

Whoops. I meant: static char[6] a = "abcdef"; char[] p = a[0..3]; char[] q = a[3..6]; Jill
Jun 08 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Tue, 8 Jun 2004 23:57:31 +0000 (UTC), Arcane Jill 
<Arcane_member pathlink.com> wrote:
 In article <opr9auakwt5a2sq9 digitalmars.com>, Regan Heath says...
 Do this:

 static char[6] a = "abcdef";
 static char[] p = a[0..3];
 static char[] q = a[3..6];



It doesn't work "non-constant expression a[0..3]".

Whoops. I meant: static char[6] a = "abcdef"; char[] p = a[0..3]; char[] q = a[3..6];

Same error. Are you trying these? I have tried a number of things, just like these you are suggesting... Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 08 2004
parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <opr9avyz0d5a2sq9 digitalmars.com>, Regan Heath says...
 static char[6] a = "abcdef";
 char[] p = a[0..3];
 char[] q = a[3..6];

Same error. Are you trying these?

Hey, that one worked for me! Look, see - here's my source code:
   int main(char[][] args)
   {
       static char[6] a = "abcdef";
       char[] p = a[0..3];
       char[] q = a[3..6];
       return 0;
   }

It compiles for me. I'm using the latest compiler, too. Jill
Jun 08 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Wed, 9 Jun 2004 00:22:10 +0000 (UTC), Arcane Jill 
<Arcane_member pathlink.com> wrote:
 In article <opr9avyz0d5a2sq9 digitalmars.com>, Regan Heath says...
 static char[6] a = "abcdef";
 char[] p = a[0..3];
 char[] q = a[3..6];

Same error. Are you trying these?

Hey, that one worked for me! Look, see - here's my source code:
   int main(char[][] args)
   {
       static char[6] a = "abcdef";
       char[] p = a[0..3];
       char[] q = a[3..6];
       return 0;
   }

It compiles for me. I'm using the latest compiler, too.

My appologies, I need the static array to me at module level, my code reads: static char[6] a = "abcdef"; char[] p = a[0..3]; char[] q = a[3..6]; int main(char[][] args) { return 0; } Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 08 2004
prev sibling parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Regan Heath schrieb:

 Whoops, I posted this is so totally the wrong group. My appologies. And 
 now I'm cross-posting.. can the kind people in digitalmars.D please read 
 this. :)
 
 On Wed, 09 Jun 2004 10:34:53 +1200, Regan Heath <regan netwin.co.nz> wrote:
 
 Hi,

 Ok basically I have a big static array, into which I want 4 static 
 slices.

 I thought this would work...

 static char[6] a = "abcdef";
 static char[3] p = a[0..3];
 static char[3] q = a[3..6];


I think you should report it to D.bugs. Intuitive things should work unless there is a strong argument somewhere against.
 but, the slices are char[] and you cannot assign a char[] to a char[3] 
 (even if they are the same length). So I tried...

 static char[6] a = "abcdef";
 static char[3] p = cast(char[3])a[0..3];
 static char[3] q = cast(char[3])a[3..6];

 and now it does not like it because cast(char[3])a[0..3] is a non 
 constant expression.

 So, is there any way to define a bunch of static slices into a static 
 array?


 I did get this to compile...

 static char[6] a = "abcdef";
 char[3] p;
 char[3] q;

 static this() {
     p[] = a[0..3];
     q[] = a[3..6];
 }

 int main(char[][] args) {
     return 0;
 }

 but, I am not 100% certain I know what it's doing exactly? is it 
 copying? or just taking a static slice...


It is definately not copying, it's only taking a slice into a static storage. You can use .dup on a slice to make a real copy. BTW, why do you use static at a module level? It doesn't work the same as in C. I think it simply gets ignored at the module level. In D you use keyword "private" for that. Refer to doc section on attributes for details. -eye
Jun 09 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Wed, 09 Jun 2004 11:04:51 +0200, Ilya Minkov <minkov cs.tum.edu> wrote:
 Regan Heath schrieb:

 Whoops, I posted this is so totally the wrong group. My appologies. And 
 now I'm cross-posting.. can the kind people in digitalmars.D please 
 read this. :)

 On Wed, 09 Jun 2004 10:34:53 +1200, Regan Heath <regan netwin.co.nz> 
 wrote:

 Hi,

 Ok basically I have a big static array, into which I want 4 static 
 slices.

 I thought this would work...

 static char[6] a = "abcdef";
 static char[3] p = a[0..3];
 static char[3] q = a[3..6];


I think you should report it to D.bugs. Intuitive things should work unless there is a strong argument somewhere against.

I will.
 but, the slices are char[] and you cannot assign a char[] to a char[3] 
 (even if they are the same length). So I tried...

 static char[6] a = "abcdef";
 static char[3] p = cast(char[3])a[0..3];
 static char[3] q = cast(char[3])a[3..6];

 and now it does not like it because cast(char[3])a[0..3] is a non 
 constant expression.

 So, is there any way to define a bunch of static slices into a static 
 array?


 I did get this to compile...

 static char[6] a = "abcdef";
 char[3] p;
 char[3] q;

 static this() {
     p[] = a[0..3];
     q[] = a[3..6];
 }

 int main(char[][] args) {
     return 0;
 }

 but, I am not 100% certain I know what it's doing exactly? is it 
 copying? or just taking a static slice...


It is definately not copying, it's only taking a slice into a static storage. You can use .dup on a slice to make a real copy. BTW, why do you use static at a module level? It doesn't work the same as in C.

That is my problem.. I am actually trying to define an array whose contents cannot be modified.
 I think it simply gets ignored at the module level. In D you use keyword 
 "private" for that. Refer to doc section on attributes for details.

Thanks, but I'm not trying to 'hide' the array, just make it const. Regan
 -eye

-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 09 2004
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Regan Heath schrieb:
 BTW, why do you use static at a module level? It doesn't work the same 
 as in C.

That is my problem.. I am actually trying to define an array whose contents cannot be modified.

 I think it simply gets ignored at the module level. In D you use 
 keyword "private" for that. Refer to doc section on attributes for 
 details.

Thanks, but I'm not trying to 'hide' the array, just make it const.

Already tried the const storage modifier on the original array? According to my understanding of specification, it should place it into constant storage, and the application should segfault or assert whenever trying to write into it. -eye
Jun 10 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Thu, 10 Jun 2004 20:51:09 +0200, Ilya Minkov <minkov cs.tum.edu> wrote:
 Regan Heath schrieb:
 BTW, why do you use static at a module level? It doesn't work the same 
 as in C.

That is my problem.. I am actually trying to define an array whose contents cannot be modified.

 I think it simply gets ignored at the module level. In D you use 
 keyword "private" for that. Refer to doc section on attributes for 
 details.

Thanks, but I'm not trying to 'hide' the array, just make it const.

Already tried the const storage modifier on the original array?

Yes.
 According to my understanding of specification, it should place it into 
 constant storage, and the application should segfault or assert whenever 
 trying to write into it.

It does not. I believe this.. const int[3] aa = [ 1,2,3 ]; defines a constant array reference to non constant data. Meaning you can modify the data, but you cannot go.. int[] bb = [ 1,2,3,4,5 ]; aa = bb; Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 10 2004