slang-users mailing list

[2019 Date Index] [2019 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] How to unload a module


Bernd Eggink <bernd.eggink@xxxxxxxxxx> wrote:
>slsh> !echo "typedef struct { x,y } Point;">t.sl
>slsh> .load ./t.sl
>slsh> .load ./t.sl
> Type name Point already exists

You can test for its existence in the code:

   ifnot (is_defined ("Point")) typedef struct { x, y } Point;

Since typedefs are defined globally, the interpreter will not allow
them to be redefined.

Typdefed stuctures are useful if you want to define unary and binary
operations for them, e.g., for a vector type where such operations are
meaningful.  If that is not the intent,  I find it more convenient
to just use a simple untyped structure:

   variable Point_t = struct {x, y};
       .
       .
    p = @Point_t;    % Create an instance of Point_t
    p.x = 1; p.y = 3;

If you want to create an array of points, then you will want to take
advantage of the interpreter's support for array arithmetic.  So,
instead of:

    typedef struct {x, y} Point_A;
    A = Point_A[5];
    for (i = 0; i < 5; i++)
      {
         A[i].x = i;
	 A[i].y = sin(A[i].x);
      }

It is better to do:

   typedef struct {x, y} Point_B;
   B = @Point_B;
   B.x = [1:5];  B.y = sin(B.x);

The difference is that `A' is an array of 5 Point_A objects, whereas B
is a single Point_B object with array-valued fields.   In practice, I
think that you will find the Point_B object to be much more useful.

Finally note that Point_B need not be a separate type.  It could be a
simple structure as mentioned above:

   Point_B = struct {x, y};

--John
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


[2019 date index] [2019 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]