Re: Elegant structures (long)

new topic     » topic index » view thread      » older message » newer message

First,  I'll call them records (you'll see why later).

Assume the problem solved in this manner:
[Euphoria with records]

record point
    integer x,y
end record

point a

a.x=1 a.y=12


That is equivalent to "Normal Euphoria":

constant point_x=1, point_y=2, point_length=2

type point(object x)
atom res
   res=false
   if sequence(x) then
      if length(x)=point_length then
         res=integer(x[point_x]) and integer(x[point_y])
      end
   end
   return res
end type

point a

a=repeat(0,point_length)
a[point_x]=1 a[point_y]=12  -- was: a.x=1  a.y=12

So...

a.x=1 and a.y=12 are nothing but convenient mnemonics for
a[point_x]=1 and a[point_y]=12,  which are themselves mnemonics
for:
  a[1] = 1  -- [1] holds x-coordinate
  a[2] = 12 -- [2] holds y-coordinate

So,

1. A record is a sequence to which a sequence of symbolic names, here,
   {"x","y"}, has been associated to represent its indexing range, here
   [1..2].

2. Those symbolic names represent integer constants *local* to the
record
   type.

3. There is one set of such symbolic names (local constants) for
   each dimension of the sequence.

Further:
-------

Nothing prevents a sequence from containing routine id's. For instance:
[Euphoria with records]

record point
  integer x,y
  procedure draw()
   -- your code here
  end procedure
end record

point a

a.x=1 a.y=1
a.draw()


[Normal Euphoria]:

procedure point_draw()
  -- your code here
end procedure

constant point_x=1, point_y=2, point_draw_id=3,  point_length=3

type point(object x)
atom res
   res=false
   if sequence(x) then
      if length(x)=point_length then
         res=integer(x[point_x]) and integer(x[point_y])
             and x[point_draw_id]=routine_id("point_draw")
      end
   end
   return res
end type

point a

a = repeat(0,point_length)
a[point_draw_id]=routine_id("point_draw")

call_proc(a[point_draw_id])


Note how records have now become objects (that is why I chose
the term "record" rather than "structure" -- in Oberon, "record"
covers both Pascal records and objects)

Note how the declaration "point a" in "Euphoria with records"
now clearly necessitates the implicit initialization of a. And
I now realize, belatedly, that the "record" declaration should
automatically generate the definition of a type() function, too.

Let me stop soon, before going into hairy questions such as:
should a record distinguish between variables and routine id's
(a.k.a. methods) by keeping all variables in one sequence, and
all  methods in another? The answers seem quite beyond me.
Plus, if I keep indulging in these theoretical considerations,
I'll never get that racing database going.

Jacques Guy

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu