www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - One more problem with operator overloading

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
:(

The thing i was writing is an Array template, to be exact its
specialisation for a one dimensional array.
The idea was to give Array all functionallity of normal arrays
+ more.

With normal arrays i can do:

int[] numbers = new int[10];
numbers[] = 3;
//and assign 3 to all elements!

So i thought it would be possible too for a class.
Array!(int,1) A1 = new Array!(int,1)(10);
A1[] = 3;

To achieve this i overloaded
int opIndexAssign(int value)
{
//here i assigne value to all elements of the
//internal array
}

But this doesn't work! The error from compiler
is: Array cannot be sliced with []!
But i am not trying to do slicing, but a indexAssign
with no index!

Ofcourse if there was opAssign i could use that.
But i thought that this way with opIndexAssign(int value)
is even nicer.

Does it make sence that you can create:
opIndexAssign(int value,...);
and use it
A1[1,2,3,4,5,6,7,8,9,0,1,2,3] = 3;
But you can't create
opIndexAssign(int value); ?

I know that the spec says opIndexAssign takes
two arguments but can this be changed so it can
take only one?
Jun 25 2004
parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
Did you try to use opSlice instead? Up to now, the [] was not an indexing
but a slicing operator. Now, with multidimensional indexing operators, one
could argue in favor of a zero-dimensional indexing operator, but I see
little difference between defining a function
        opIndexAssign(value)
or
        opSliceAssign(value)

B.t.w: is there a opSliceAssign at all? Now, that we have multidimensional
indexing, we should really think about multidimensional slicing operators
at well. I have no good idea how to do it, though:

* It should be flexible enough to leave room for strides, since I really
hope that strided slicing gets into the language some day.

* It should allow mixing indices and slices like in A[3,4..5,6]

Maybe, instead of extending opSlice, we could make 4..5 an expression
returning a

struct range(int) {
        int min;
        int max;
}

But then, that struct would have to be defined in the language specs, which
would be somewhat awkward.

Any ideas?




Ivan Senji wrote:

 :(
 
 The thing i was writing is an Array template, to be exact its
 specialisation for a one dimensional array.
 The idea was to give Array all functionallity of normal arrays
 + more.
 
 With normal arrays i can do:
 
 int[] numbers = new int[10];
 numbers[] = 3;
 //and assign 3 to all elements!
 
 So i thought it would be possible too for a class.
 Array!(int,1) A1 = new Array!(int,1)(10);
 A1[] = 3;
 
 To achieve this i overloaded
 int opIndexAssign(int value)
 {
 //here i assigne value to all elements of the
 //internal array
 }
 
 But this doesn't work! The error from compiler
 is: Array cannot be sliced with []!
 But i am not trying to do slicing, but a indexAssign
 with no index!
 
 Ofcourse if there was opAssign i could use that.
 But i thought that this way with opIndexAssign(int value)
 is even nicer.
 
 Does it make sence that you can create:
 opIndexAssign(int value,...);
 and use it
 A1[1,2,3,4,5,6,7,8,9,0,1,2,3] = 3;
 But you can't create
 opIndexAssign(int value); ?
 
 I know that the spec says opIndexAssign takes
 two arguments but can this be changed so it can
 take only one?
Jun 25 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Norbert Nemec" <Norbert.Nemec gmx.de> wrote in message
news:cbgk9f$11ba$1 digitaldaemon.com...
 Did you try to use opSlice instead? Up to now, the [] was not an indexing
 but a slicing operator. Now, with multidimensional indexing operators, one
 could argue in favor of a zero-dimensional indexing operator, but I see
 little difference between defining a function
Yes, it is not possible beacuse i get a message something like Array[] is not an lvalue. Zero dimensional operator would be great, but opSliceAssign would be a solution too!
         opIndexAssign(value)
 or
         opSliceAssign(value)

 B.t.w: is there a opSliceAssign at all? Now, that we have multidimensional
 indexing, we should really think about multidimensional slicing operators
 at well. I have no good idea how to do it, though:
No there is no opSliceAssign :(
 * It should be flexible enough to leave room for strides, since I really
 hope that strided slicing gets into the language some day.

 * It should allow mixing indices and slices like in A[3,4..5,6]

 Maybe, instead of extending opSlice, we could make 4..5 an expression
 returning a

 struct range(int) {
         int min;
         int max;
 }

 But then, that struct would have to be defined in the language specs,
which
 would be somewhat awkward.

 Any ideas?
No ideas yet. Except that your * and * would be great :)
Jun 25 2004