[DGD]Adding new data type

Par Winzell zell at skotos.net
Tue Apr 25 23:54:41 CEST 2000


Lorrimar writes:
 > I want to make a new variable type called "vector". The vector represents a
 > position in 3D space and you should be able to do stuff like this to it:

 > vector a, b, c;
 > 
 > a.x = 5; a.y = -10; a.z = 5; // assignment to individual elements
 > b.x = 1; b.y = 4; b.z = -3;
 > 
 > c = a + b; // addition
 > c = {[ 1, 2, 3 ]}; // assignment of 3 element array to vector
 > c = a + 5; // addition of a scalar
 > c = 2 * a; // scalar multiplication

Another alternative is

 # define vector (int *)
 # define X(v) ((v)[0])
 # define Y(v) ((v)[1])
 # define Z(v) ((v)[2])
 # define new(x, y, z) ({ x, y, z })
 # define add(a, b)    ({ a[0]+b[0], a[1]+b[1], a[2]+b[2] })

 ... etc

and

 vector a, b, c;
 
 X(a) = 5; Y(a) = -10; Z(a) = 5; // assignment to individual elements
 X(b) = 1; Y(b) = 4;   Z(b) = -3;
 
 c = add(a, b); // addition
 c = new(1, 2, 3); // assignment of 3 element array to vector

 etc

 > Does anyone have opinions on the advantages/disadvantages of each method? In
 > the long run I would like to have a vector data type but I'm worried that it
 > may be a little difficult.

It's a bit of a chore. I'd use macros either the way outlined above or
in combination with the object approach.

Pär

List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list