Jump to page: 1 2
Thread overview
How to define a const?/static?/final? array.
Jun 09, 2004
Regan Heath
Jun 09, 2004
J Anderson
Jun 09, 2004
Regan Heath
Jun 09, 2004
J Anderson
Jun 10, 2004
Regan Heath
Jun 10, 2004
J Anderson
Jun 10, 2004
Regan Heath
Jun 10, 2004
Regan Heath
Jun 10, 2004
Matthew
Jun 10, 2004
Kris
Jun 10, 2004
Regan Heath
Jun 10, 2004
Regan Heath
Jun 10, 2004
Matthew
Jun 10, 2004
Kris
Jun 10, 2004
J Anderson
Jun 10, 2004
Regan Heath
Jun 10, 2004
J Anderson
Jun 13, 2004
Sam McCall
Jun 14, 2004
Regan Heath
Jun 14, 2004
Sam McCall
June 09, 2004
I want to define an array whose contents cannot be modified. I tried const, final, static .. I searched the site and didn't find any help, 'final' does not seem to be documented.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
June 09, 2004
Regan Heath wrote:

> I want to define an array whose contents cannot be modified. I tried const, final, static .. I searched the site and didn't find any help, 'final' does not seem to be documented.
>
> Regan

I suggested an approach to this a while ago.  Simply wrap the array in a struct and use the operators.  You can even use a template to save you from having to write the code again.

-- 
-Anderson: http://badmama.com.au/~anderson/
June 09, 2004
On Thu, 10 Jun 2004 07:19:01 +0800, J Anderson <REMOVEanderson@badmama.com.au> wrote:
> Regan Heath wrote:
>
>> I want to define an array whose contents cannot be modified. I tried const, final, static .. I searched the site and didn't find any help, 'final' does not seem to be documented.
>>
>> Regan
>
> I suggested an approach to this a while ago.  Simply wrap the array in a struct and use the operators.  You can even use a template to save you from having to write the code again.

Eeeew! This should be simple, not complicated, and it should be intuitive. Why can't we have a keyword?

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
June 09, 2004
Regan Heath wrote:

> On Thu, 10 Jun 2004 07:19:01 +0800, J Anderson <REMOVEanderson@badmama.com.au> wrote:
>
>> Regan Heath wrote:
>>
>>> I want to define an array whose contents cannot be modified. I tried const, final, static .. I searched the site and didn't find any help, 'final' does not seem to be documented.
>>>
>>> Regan
>>
>>
>> I suggested an approach to this a while ago.  Simply wrap the array in a struct and use the operators.  You can even use a template to save you from having to write the code again.
>
>
> Eeeew! This should be simple, not complicated, and it should be intuitive. Why can't we have a keyword?

Because the programmer has to do something <g>.

>
> Regan
>


//module 1
template constArray(T) //This would be in another module
{
  struct Array
  {
      private T [] array;
            static Array opCall(T [] array)
      {
          Array t;
          t.array = array;
          return t;
      }
      T opIndex(int i) { return array[i]; }
      int length() { return array.length; }
  }
}


Once you have it setup (you only ever need to do that once) it looks like:

alias constArray!(int).Array constA;

//module 2

void test(constA array)
{
  int x = array[0];

//You just can't change the array variable (well at least not without force).

}

void main()
{
  int [] a;
  a ~= 10;
  a ~= 30;
  test(constA(a)); //Ok there's a small bit of extra typing -> if D only had automatic boxing....
}


-- 
-Anderson: http://badmama.com.au/~anderson/
June 10, 2004
On Thu, 10 Jun 2004 07:59:15 +0800, J Anderson <REMOVEanderson@badmama.com.au> wrote:
> Regan Heath wrote:
>
>> On Thu, 10 Jun 2004 07:19:01 +0800, J Anderson <REMOVEanderson@badmama.com.au> wrote:
>>
>>> Regan Heath wrote:
>>>
>>>> I want to define an array whose contents cannot be modified. I tried const, final, static .. I searched the site and didn't find any help, 'final' does not seem to be documented.
>>>>
>>>> Regan
>>>
>>>
>>> I suggested an approach to this a while ago.  Simply wrap the array in a struct and use the operators.  You can even use a template to save you from having to write the code again.
>>
>>
>> Eeeew! This should be simple, not complicated, and it should be intuitive. Why can't we have a keyword?
>
> Because the programmer has to do something <g>.

Thanks for the example. I still think this is just plain wrong. In C/C++ it's this simple:

const int pp[10] = {
	0,1,2,3,4,5,6,7,8,9
};

void main( )
{
	pp[0] = 5; //error C2166: l-value specifies const object
}

>> Regan
>>
>
>
> //module 1
> template constArray(T) //This would be in another module
> {
>    struct Array
>    {
>        private T [] array;
>              static Array opCall(T [] array)
>        {
>            Array t;
>            t.array = array;
>            return t;
>        }
>        T opIndex(int i) { return array[i]; }
>        int length() { return array.length; }
>    }
> }
>
>
> Once you have it setup (you only ever need to do that once) it looks like:
>
> alias constArray!(int).Array constA;
>
> //module 2
>
> void test(constA array)
> {
>    int x = array[0];
>
> //You just can't change the array variable (well at least not without force).
>
> }
>
> void main()
> {
>    int [] a;
>    a ~= 10;
>    a ~= 30;
>    test(constA(a)); //Ok there's a small bit of extra typing -> if D only had automatic boxing....
> }
>
>



-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
June 10, 2004
Regan Heath wrote:

> Thanks for the example. I still think this is just plain wrong. In C/C++ it's this simple:
>
> const int pp[10] = {
>     0,1,2,3,4,5,6,7,8,9
> };
>
> void main( )
> {
>     pp[0] = 5; //error C2166: l-value specifies const object
> }


Oh, I though you where talking about parmeter const. 

You can do this:

const int pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];

void main( )
{
   pp[0] = 5; //This is ok in D
}

But you can change the values but not the length.  So my first suggestion still stands.

-- 
-Anderson: http://badmama.com.au/~anderson/
June 10, 2004
On Thu, 10 Jun 2004 08:19:41 +0800, J Anderson <REMOVEanderson@badmama.com.au> wrote:
> Regan Heath wrote:
>
>> Thanks for the example. I still think this is just plain wrong. In C/C++ it's this simple:
>>
>> const int pp[10] = {
>>     0,1,2,3,4,5,6,7,8,9
>> };
>>
>> void main( )
>> {
>>     pp[0] = 5; //error C2166: l-value specifies const object
>> }
>
>
> Oh, I though you where talking about parmeter const. You can do this:
>
> const int pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];
>
> void main( )
> {
>     pp[0] = 5; //This is ok in D
> }
>
> But you can change the values but not the length.

So the 'const' applies to the array reference and not the data.
Seems we need a new syntax to apply const to the array and/or the data, something like:

const int const pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  //const array const data
const int pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  // const data
int const pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  // const array

I know, I have reversed the current syntax meaning. I think the above is how it should work however. Any other suggestions?

> So my first suggestion still stands.

It works.. it just seems wrong to be unable to do this with a keyword.

Why can't we?

I have read the posts about 'const' some people like it, some hate it, we have const for data types, but in the case of arrays (as above) it only applies to the reference, not the data itself.

All I want to be able to do is specify some data that cannot be changed.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
June 10, 2004
On Thu, 10 Jun 2004 13:10:31 +1200, Regan Heath <regan@netwin.co.nz> wrote:

> On Thu, 10 Jun 2004 08:19:41 +0800, J Anderson <REMOVEanderson@badmama.com.au> wrote:
>> Regan Heath wrote:
>>
>>> Thanks for the example. I still think this is just plain wrong. In C/C++ it's this simple:
>>>
>>> const int pp[10] = {
>>>     0,1,2,3,4,5,6,7,8,9
>>> };
>>>
>>> void main( )
>>> {
>>>     pp[0] = 5; //error C2166: l-value specifies const object
>>> }
>>
>>
>> Oh, I though you where talking about parmeter const. You can do this:
>>
>> const int pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];
>>
>> void main( )
>> {
>>     pp[0] = 5; //This is ok in D
>> }
>>
>> But you can change the values but not the length.
>
> So the 'const' applies to the array reference and not the data.
> Seems we need a new syntax to apply const to the array and/or the data, something like:
>
> const int const pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  //const array const data
> const int pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  // const data
> int const pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  // const array

Let me revice this, as it's not the D style...

const int[] const pp = [ 0,1,2,3,4,5,6,7,8,9 ];  //const array const data
const int[] pp = [ 0,1,2,3,4,5,6,7,8,9 ];  // const data
int[] const pp = [ 0,1,2,3,4,5,6,7,8,9 ];  // const array

I think this syntax makes more sense.

> I know, I have reversed the current syntax meaning. I think the above is how it should work however. Any other suggestions?
>
>> So my first suggestion still stands.
>
> It works.. it just seems wrong to be unable to do this with a keyword.
>
> Why can't we?
>
> I have read the posts about 'const' some people like it, some hate it, we have const for data types, but in the case of arrays (as above) it only applies to the reference, not the data itself.
>
> All I want to be able to do is specify some data that cannot be changed.
>
> Regan
>



-- 
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:opr9ctjtys5a2sq9@digitalmars.com...
> On Thu, 10 Jun 2004 08:19:41 +0800, J Anderson <REMOVEanderson@badmama.com.au> wrote:
> > Regan Heath wrote:
> >
> >> Thanks for the example. I still think this is just plain wrong. In C/C++ it's this simple:
> >>
> >> const int pp[10] = {
> >>     0,1,2,3,4,5,6,7,8,9
> >> };
> >>
> >> void main( )
> >> {
> >>     pp[0] = 5; //error C2166: l-value specifies const object
> >> }
> >
> >
> > Oh, I though you where talking about parmeter const. You can do this:
> >
> > const int pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];
> >
> > void main( )
> > {
> >     pp[0] = 5; //This is ok in D
> > }
> >
> > But you can change the values but not the length.
>
> So the 'const' applies to the array reference and not the data.
> Seems we need a new syntax to apply const to the array and/or the data,
> something like:
>
> const int const pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  //const array const data
> const int pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  // const data
> int const pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  // const array

Mind reader. :-)

> I know, I have reversed the current syntax meaning. I think the above is how it should work however. Any other suggestions?
>
> > So my first suggestion still stands.
>
> It works.. it just seems wrong to be unable to do this with a keyword.

Agreed

> Why can't we?
>
> I have read the posts about 'const' some people like it, some hate it, we have const for data types, but in the case of arrays (as above) it only applies to the reference, not the data itself.

Most of the debates - about using const (C++-sense) for DbC - are not germane to
your requirement. I agree that something const (in the D sense) should be
declarable in array form.

Is there any need for the second or third of your pp definitions above? Perhaps the language should simply use const with arrays to mean const size + const data.

Of course, if someone can offer good reasons against, then a more complex syntax may be required. I'd shy away from the const x const y syntax, if at all possible, however, simply because it's confusing.

How about (and it's right off the top of my head, so ...):

    const(int pp[]) = [ 0,1,2,3,4,5,6,7,8,9 ];  //const array const data
    const(int) pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  // const data
    int const(pp[]) = [ 0,1,2,3,4,5,6,7,8,9 ];  // const array

?



June 10, 2004
"Matthew" wrote
> Of course, if someone can offer good reasons against, then a more complex
syntax
> may be required. I'd shy away from the const x const y syntax, if at all possible, however, simply because it's confusing.
>
> How about (and it's right off the top of my head, so ...):
>
>     const(int pp[]) = [ 0,1,2,3,4,5,6,7,8,9 ];  //const array const data
>     const(int) pp[] = [ 0,1,2,3,4,5,6,7,8,9 ];  // const data
>     int const(pp[]) = [ 0,1,2,3,4,5,6,7,8,9 ];  // const array

It would seem quite reasonable to, as others have noted, use the keyword 'final' as a storage class indicator. That is, where you have data that is read-only, immutable, constant (whatever one wishes to call it) the 'final' qualifier would do the "right thing". That includes isolating said data into a separate, potentially ROMable, section. Of course, this doesn't address the related debate regarding read-only function arguments ...

Rather than "const const" syntax, what do you think about a "final const" combo instead? Personally, I don't care much for the parenthesized declaration ...


« First   ‹ Prev
1 2