June 09, 2004
In article <opr9a9juyw5a2sq9@digitalmars.com>, Regan Heath says...

>My appologies, I need the static array to me at module level

This works, although I do concede it's messy:

>   static char[6] a = "abcdef";
>   template TMySlices()
>   {
>       const char[] p = a[0..3];
>       const char[] q = a[3..6];
>   }
>
>   int main(char[][] args)
>   {
>       mixin TMySlices;
>       printf("%.*s\n",p);
>       return 0;
>   }

Prints abc.
Jill


June 09, 2004
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
June 09, 2004
On Wed, 9 Jun 2004 08:46:24 +0200, Ivan Senji <ivan.senji@public.srce.hr> wrote:
> "Regan Heath" <regan@netwin.co.nz> wrote in message
> news:opr9a9juyw5a2sq9@digitalmars.com...
>> 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:
>
> I don't know is this what you want but:
>
> static char[6] a = "abcdef";
> char[] p;
> char[] q;
>
> static this()
> {
>      p = a[0..3];
>     q = a[3..6];
> }
>
> int main(char[][] args)
> {
> return 0;
> }

See my original post, I had just that.. I am unsure what it's doing however. Is it making a copy or just slicing into the array? My tests seemed to indicate it was just slicing, I just want to be sure. :)

>> 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/
>
>



-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
June 09, 2004
On Wed, 9 Jun 2004 07:42:33 +0000 (UTC), Arcane Jill <Arcane_member@pathlink.com> wrote:
> In article <opr9a9juyw5a2sq9@digitalmars.com>, Regan Heath says...
>
>> My appologies, I need the static array to me at module level
>
> This works, although I do concede it's messy:
>
>>   static char[6] a = "abcdef";
>>   template TMySlices()
>>   {
>>       const char[] p = a[0..3];
>>       const char[] q = a[3..6];
>>   }
>>
>>   int main(char[][] args)
>>   {
>>       mixin TMySlices;
>>       printf("%.*s\n",p);
>>       return 0;
>>   }
>
> Prints abc.

:) interesting idea. You're right it seems messy. I think I will go with the module level static this, it seems to do what I want.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
June 09, 2004
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/
June 10, 2004
"Regan Heath" <regan@netwin.co.nz> wrote in message news:opr9cnrxza5a2sq9@digitalmars.com...
> On Wed, 9 Jun 2004 08:46:24 +0200, Ivan Senji <ivan.senji@public.srce.hr> wrote:
> > "Regan Heath" <regan@netwin.co.nz> wrote in message news:opr9a9juyw5a2sq9@digitalmars.com...
> >> 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:
> >
> > I don't know is this what you want but:
> >
> > static char[6] a = "abcdef";
> > char[] p;
> > char[] q;
> >
> > static this()
> > {
> >      p = a[0..3];
> >     q = a[3..6];
> > }
> >
> > int main(char[][] args)
> > {
> > return 0;
> > }
>
> See my original post, I had just that.. I am unsure what it's doing however. Is it making a copy or just slicing into the array? My tests seemed to indicate it was just slicing, I just want to be sure. :)

OOPS! Sorry! I somehow missed that part of your post :)
Yes it just slices. If you wanted a copy it would have to be
explicit: p = a[0..3].dup;

> >> 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/
> >
> >
>
>
>
> --
> Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


June 10, 2004
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
June 10, 2004
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/
1 2
Next ›   Last »