Multiple possreps initialization

This forum is for any questions about the language Tutorial D or the Rel implementation of it.
Post Reply
whyleee
Posts: 2
Joined: Sun Sep 12, 2010 7:04 am

Multiple possreps initialization

Post by whyleee »

Hello. I don't understand how I can create a new type in Rel with multiple possreps. Here is a code example from Date's "Introduction to DB Systems" that I tried to execute in Rel:

Code: Select all

TYPE Point POSSREP Cartesian { x RATIONAL, y RATIONAL }
                    POSSREP Polar { r RATIONAL, o RATIONAL };
The error is:
ERROR: POSSREP 'Cartesian' has no INITialisation.


I don't know how I can make a right initialization. I tried this:

Code: Select all

TYPE Point POSSREP Cartesian { x RATIONAL, y RATIONAL } POSSREP Polar { r RATIONAL, o RATIONAL } INIT Cartesian ( x := 0.0, y := 0.0 );
But now I have this error:
ERROR: Component 'y' cannot be assigned because it is set via the selector.


Can anyone explain how I should do the right initialization, please?
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Multiple possreps initialization

Post by Dave »

Try this:

Code: Select all

TYPE Point
       POSSREP cartesian {X RATIONAL, Y RATIONAL}
       POSSREP polar {R RATIONAL, THETA RATIONAL}
INIT
       cartesian (R := SQRT(X * X + Y * Y), THETA := ATAN(Y / X))
       polar (X := R * COS(THETA), Y := R * SIN(THETA));
The INIT section defines each POSSREP in terms of the value being selected. If you select a cartesian Point, it initialises the polar POSSREP from the cartesian Point. If you select a polar Point, it initialises the cartesian POSSREP from the polar Point.

NOTE: The above example depends on the OperatorsMath.d script having been loaded and executed from the Scripts directory. It defines SQRT, ATAN, COS, SIN, and many other math operators.

Here's an example of a type with three POSSREPs:

Code: Select all

TYPE Temperature
      POSSREP Celsius {celsius RATIONAL}
      POSSREP Kelvin {kelvin RATIONAL}
      POSSREP Fahrenheit {fahrenheit RATIONAL}
  INIT
      Celsius (kelvin := celsius + 273.15, fahrenheit := (9.0 / 5.0) * celsius + 32.0)
      Kelvin (celsius := kelvin - 273.15, fahrenheit := ((kelvin - 273.15) * (9.0 / 5.0)) + 32.0)
      Fahrenheit (kelvin := (5.0 / 9.0 * (fahrenheit - 32.0) + 273.15), celsius := (5.0 / 9.0) * (fahrenheit - 32.0));
whyleee
Posts: 2
Joined: Sun Sep 12, 2010 7:04 am

Re: Multiple possreps initialization

Post by whyleee »

Thank you very much! Now I understand what the <INIT> statement do. Code that you posted works.
Post Reply