April 29, 2004
Norbert Nemec wrote:

> Ben Hinkle wrote:
> 
>> [snip]
>>>         a[1,2] = x;
>>> with a being one of my user-defined arrays? opIndex works only for one
>>> index-argument. Making that argument an array of two integers makes no
>>> sense, since we don't have array literals. opCall cannot return anything
>>> that I could assign to.
>> [snip]
>> 
>> It might not be too bad to have opCall return a pointer and have users explicitly dereference the pointer:
>> 
>> struct test {
>>   double[9] d;
>>   double* opCall(int n,int m) {
>>     return &(x[3*n+m]);
>>   }
>> }
>> 
>> int main() {
>>   test x;
>>   *x(1,1) = 41.0;
>>   printf("%g\n",*x(1,1));
>>   return 0;
>> }
> 
> As a crude hack for the moment - fine. Beyond that: it will be very confusing to the user, who expects x(1,1) to return a value in an expression.

you've probably already thought of this one, but another approach is something like

struct pair {
  int m,n;
  static pair opCall(int m, int n) {
    pair ret;ret.m = m;ret.n = n;return ret;
  }
}

struct Matrix {
  double[3][3] d;
  void opIndex(pair i, double y) {
    d[i.m][i.n] = y;
  }
  double opIndex(pair i) {
    return d[i.m][i.n];
  }
}

int main() {
  Matrix x;
  x[pair(1,1)] = 41.0;
  printf("%g\n",x[pair(1,1)]);
  return 0;
}

>> The opCall in C++ would be replaced with n-d indexing and the pointer would be replaced with a reference.
> 
> In C++, there is no n-d indexing either. That suggestion would be something new in D.

oh yeah - I forgot there isn't n-d in C++. d'oh.
-Ben
(ps - you should be using MATLAB for matrices anyway! just kidding)
May 05, 2004
I checked, and it is possible to create an opIndex where the index is a static-length array.  Of course, the syntax for actually using this is very ugly, since, as you note, we don't have array literals.

Rather than rewrite opIndex, it seems like this is a compelling argument for array literals.

Or, we could ask Walter to support multidimensional opIndex, where the multiple indicies are passed as a static array.

Russ

Norbert Nemec wrote:
> I'm struggling with implementing multidimensional arrays in the library. It
> definitely is a messy task, trying to get some pretty syntax for the user!
> 
> The current intension of my work is something like a case-study, to
> demonstrate a way, how multidimensional arrays might be defined as a
> language feature in the future. My initial hope, to create some library
> that might be usable as such faded very quickly. In C++, I could certainly
> do it. In D, I'm either far from understanding the language at all, or the
> language is far from being as powerful as C++ in the current state.
> 
> "Powerful" in this context means: The possibility, to write a library that
> offers an expressive syntax to the user.
> 
> Now, I'm already stuck at a simple point of assigning a value to a cell. How
> should a define something remotely similar to
> 
>         a[1,2] = x;
> 
> with a being one of my user-defined arrays? opIndex works only for one
> index-argument. Making that argument an array of two integers makes no
> sense, since we don't have array literals. opCall cannot return anything
> that I could assign to. So, affectively, I'm stuck with something like
> 
>         a.assign(1,2,x);
> 
> (I could probably do a[1][2] = x for my user-defined array, making opIndex
> return an array of lower dimesionality, but that would be highly
> inefficient and lead in a completely wrong direction.)
> 
> Now, to turn my frustration into productivity, I have an suggestion about a
> language extension:
> 
> * opIndex with more than one index parameter. To avoid ambiguity, the
> current "opIndex(idx,value)" should probably be renamed to something like
> "opIndexAssign".
> 
> This will be a simple, straightforward extension. If we have multidim-arrays
> in the language some day in the future, this operator would be reasonable
> anyway, if not, it is necessary even more, to be able to define these
> arrays in the library.
> 
> 

May 18, 2004
"Norbert Nemec" <Norbert.Nemec@gmx.de> wrote in message news:c6p0fd$12v8$1@digitaldaemon.com...
> I'm struggling with implementing multidimensional arrays in the library.
It
> definitely is a messy task, trying to get some pretty syntax for the user!
>
> The current intension of my work is something like a case-study, to demonstrate a way, how multidimensional arrays might be defined as a language feature in the future. My initial hope, to create some library that might be usable as such faded very quickly. In C++, I could certainly do it. In D, I'm either far from understanding the language at all, or the language is far from being as powerful as C++ in the current state.

How would you do what you propose (rectangular arrays of non-constant
dimension) in C++?


May 18, 2004
Walter wrote:

> 
> "Norbert Nemec" <Norbert.Nemec@gmx.de> wrote in message news:c6p0fd$12v8$1@digitaldaemon.com...
>> I'm struggling with implementing multidimensional arrays in the library.
> It
>> definitely is a messy task, trying to get some pretty syntax for the user!
>>
>> The current intension of my work is something like a case-study, to demonstrate a way, how multidimensional arrays might be defined as a language feature in the future. My initial hope, to create some library that might be usable as such faded very quickly. In C++, I could certainly do it. In D, I'm either far from understanding the language at all, or the language is far from being as powerful as C++ in the current state.
> 
> How would you do what you propose (rectangular arrays of non-constant
> dimension) in C++?

Good idea: I think I can spend some time on D arrays next weekend. Putting together some C++ code to demonstrate my ideas might be an idea. Anyhow: my idea of multidimensional indexing is not realizable in C++ either, since assignments are done by returning references and not by some special opIndexAssign equivalent.
1 2
Next ›   Last »