Hi, there !
I've some doubts about a language employing only delegation as its
inheritance mechanism. Perhaps these doubts are simple and are answered
elsewhere (probably in some self paper I should study), or perhaps I'm even
missing something big. Anyway, I would like to have some feedback about this.
Suppose a system which includes a compiler for a protoype-based
object-oriented language, with single inheritance, implemented through
delegation.
Suppose you have a prototype such as this one (please let me use
an [imperative, classic, C++/Java-like] pseudo-code):
OBJ Person
ATR Parent Object
ATR Name ""
MTH getName
MTH putName
...
ENO
So, therefore, the object Person derives from the root prototype
object (called Object here), and has only one atributte (for the shake of
simplicity), called "Name" which should store a String (i.e., a copy of the
String prototype, which is the name of the person).
If you want to create persons, then you just copy them. The system
is in charge of copying their attributes (so, each person will have its own
name, because the attributes (except "parent") are copied recursively).
Here I suppose this is done by calling a "clone" method which is actually
defined in "Object" (so the method look up runs throgh the "Parent" atributte).
joe = Person.clone();
mary = Person.clone();
joe.putName("Joe");
mary.putName("Mary");
Console.write(joe.getName()); // Writes "Joe"
Console.write(mary.getName()); // Writes "Mary"
So far, so good. But I don't know how a language such as this one
would cope with prototypes derived from "Person". For example:
OBJ Employee
ATR Parent Person
ENO
It it semantically correct - ane employee is a class of person -,
and it would work employing a concatenation mechanism for inheritance.
If you create two objects:
joe2 = Employee.clone();
mary2 = Employee.clone();
joe2.putName("JOE");
mary2.putName("MARY");
Console.write(joe2.getName()); // Writes "MARY"
Console.write(mary2.getName()); // Writes "MARY"
These two objects, joe2, and mary2, share the shame String,
because both objects have a "parent" attribute pointing to Person. Worst of
all, joe2 and mary2 are actually modifying a prototype (an object which is
playing the role, in this case, of a class).
How can I solve this ?
Of course I can think of some work arounds, such as recreating the
"name" atribute in Employee, or using a composition relation instead of the
inheritance one, but I think this should be possible to be solved whithin
inheritance/delegation.
Thank you in advance,
j.Baltasar
=======================================
PBC - J. Baltasar García Perez-Schofield
Dep. Informática, Universidad de Vigo, España (Spain)