Classes: A small proposal

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

Here's a wish list for item for Robert: classes for Euphoria.=20

I think that the following syntax is fairly modest. I give examples of =
what a pre-processor could turn it into to make it acceptable to the =
current Euphoria compiler. You can still use Euphoria without any of the =
classes, and it shouldn't break existing code.


[Defining a Class and Attributes]

The following declares a class 'point' with 'base' as it's superclass:

   class point inherits base

You could declare attributes with:

   point attribute x, y

which would be internally:

   global constant point:x =3D 1, point:y =3D 2

The indexes would start at different points if the superclass had =
defined some attributes. I also used ':' here instead of '_' to make the =
code more readable.=20

Notice I cheat and don't implement type checking on the attributes. If =
this were really part of Euphoria, I'd expect something along the lines =
of:

   point attribute
      integer x,
      integer y

But I'm thinking along the lines of quick & dirty, so we'll quickly move =
along to...

[Creating Objects]

Creation of an object would look the same as any other type:

   point fred, sam

which could be internally:

   sequence fred, sam


[Defining Class Methods]

Routines could be declared as follows:

   point function func( integer x )
      self.x =3D x
   end function

While the internal declaration might actually be:

   function point:func( sequence self, integer x )
      self[point:x] =3D x
      return self
   end function

Procedures would be similar.


[Calling Class Methods]

Here's an explicit call to a class function - very atypical:

   bar =3D point:func( foo, x )

If the 'point' were left off, the interpreter would assume that the =
function was a 'base' class function.

A more typical call to a class function would look like:

   foo.func(x)

Euphoria would first check to see if there were a 'point:func' defined. =
If it did not exist, it would then check the superclass ('base:func'), =
on down the line, until the base class was reached. So you might get:
  =20
   foo =3D point:func( foo, x )

if the function was defined in the class 'point', or

   foo =3D base:func( foo, x )

if the function was defined in the class 'base', or

   function 'func' is undefined

if the function is undefined altogether.

Procedures would also be the same:

   foo.show()

could be interpreted as:

   point:show( foo )
  =20

[Summary]

That's all there is to it! For a few modest changes, you get classes, =
objects, attributes AND single level inheritance. Pre-existing code goes =
happily along without breaking. And it converts to 'normal' Euphoria =
code - so it could be implemented by adding a pre-processor onto =
Euphoria.

Comments?

-- David Cuny

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

Search



Quick Links

User menu

Not signed in.

Misc Menu