Thread overview
Suggestions
Dec 04, 2006
vincent catrino
Dec 04, 2006
Oskar Linde
Dec 04, 2006
Daniel Keep
December 04, 2006
Hi there,

I have been following D for about 1.5 year and I really like this language. I use it for casual programming and for fun. I'm a software engineer using mainly C, JAVA and FORTRAN 77.

There are a few things that I use all the time in C and JAVA and that I really would appreciate to find in the D language.

* C99 allows the following :
int func( int n, int a[n][n] ) {
   ... do something ...
}
D forbids this. It is possible to have this in D for example by
creating a structure like this.
struct Mati {
   int *ptr;
   int dim;
   int *opIndex(int i) { return ptr + i*dim; }
};
and rewriting func like this
int func( int n, int *ptr ) {
   Mati m = new Mati();
   m.ptr = ptr;
   m.dim = n;

   m[1][5] = 3;
}
Would it be possible to have this in D directly ? Or maybe there
is a cute D way to have this already and I woould be glad to learn
it.

* Indirection.
in C when you get a structure pointer you use the '->' operator
instead of '.' to access members. In D only '.' is allowed. Would
it be possible to have '->' too as a synomym for '.' ? This is
just syntaxic sugar and D works well without this but when working
in D I switch back to my C habits and naturally use '->'.

* JAVA has a base object type too which is used as the base class
for all other classes. In JAVA the basic object type provides
synchronization primitives which are quite useful :
void wait()
void wait( long timeout )
void notify()
void notifyAll()
Maybe it is already possible to do this in D and I haven't
understood how. If yes I would be glad to learn how to do so, if
not maybe these facilities might be added to the D object type ?

Best regards

Vincent
December 04, 2006
vincent catrino wrote:
> Hi there,
> 
> I have been following D for about 1.5 year and I really like this
> language. I use it for casual programming and for fun. I'm a
> software engineer using mainly C, JAVA and FORTRAN 77.
> 
> There are a few things that I use all the time in C and JAVA and
> that I really would appreciate to find in the D language.
> 
> * C99 allows the following :
> int func( int n, int a[n][n] ) {
>    ... do something ...
> }
> D forbids this. It is possible to have this in D for example by
> creating a structure like this.
> struct Mati {
>    int *ptr;
>    int dim;
>    int *opIndex(int i) { return ptr + i*dim; }
> };
> and rewriting func like this
> int func( int n, int *ptr ) {
>    Mati m = new Mati();
>    m.ptr = ptr;
>    m.dim = n;
> 
>    m[1][5] = 3;
> }
> Would it be possible to have this in D directly ? Or maybe there
> is a cute D way to have this already and I woould be glad to learn
> it.

D doesn't have dynamic multidimensional arrays built in (it may be on a TODO for >= 2.0). A better way is to consistently use a custom array type (just like you do). I.e. something like:

struct SquareMatrix {
  int *ptr;
  int dim;
  int opIndex(int i, int j) { return ptr[j+i*dim]; }
  int opIndexAssign(int v, int i, int j) { return ptr[j+i*dim] = v;}
}

(Or the Mati one above) And pass that directly to the functions:

int func(SquareMatrix m) {
	m[1,5] = 3;
	...
}

I have written several such array and matrix types and they work well in practice. The downsides are:

- No reference return type from opIndex, meaning e.g, m[1,5]++; is impossible. (You solve that quite neatly)
- Not standardized
- Mixing slicing and indexing not possible with the .. syntax

Regards,

Oskar
December 04, 2006
vincent catrino wrote:
> Hi there,

Howdy, and welcome to the NG.  Please keep your arms and legs inside the NG at all times, lest you lose them. :P

> * Indirection.
> in C when you get a structure pointer you use the '->' operator
> instead of '.' to access members. In D only '.' is allowed. Would
> it be possible to have '->' too as a synomym for '.' ? This is
> just syntaxic sugar and D works well without this but when working
> in D I switch back to my C habits and naturally use '->'.

Don't hold your breath.  It's a conscious design decision for D to avoid having many ways of saying the same thing.  Afraid you'll just have to learn to not use '->' any more :3

Look on the bright side: now you only need to remember one member access operator!

> * JAVA has a base object type too which is used as the base class
> for all other classes. In JAVA the basic object type provides
> synchronization primitives which are quite useful :
> void wait()
> void wait( long timeout )
> void notify()
> void notifyAll()
> Maybe it is already possible to do this in D and I haven't
> understood how. If yes I would be glad to learn how to do so, if
> not maybe these facilities might be added to the D object type ?

I'm not familiar with what these do, but you might want to take a peek at the std.thread module.  It's hard to say much else without knowing what those methods do (and I have no intention of learning any more Java than I already know.)

	-- Daniel