Because of the way objects work, we can do inheritance with delegation.
Here's an example. First the base:

/////////////
interface A[T] { 
  print : 1 -> 0; 
  get: 1 -> list[T]; 
}

object fred[T] (x:list[T]) implements A[T] = 
{
  method proc print () { println$ x; }
  method fun get () => x;
}

var lst = list$ 1,2,3;
var a = fred lst;

a.print();
println$ a.get();
////////////////

By the way note that the object type appears polymorphic,
as does the factory function fred.

The actual object constructed isn't, its an A[int].
In general, felix cannot handle polymorphic values.

So now here's something that works kind of like
inheritance:

////////////////
interface B[T] extends A[T] { pprint: 1 -> 0; }

object joe[T] (a : A[T]) implements B[T] = {
  method proc print () { a.print(); }
  method fun get () => a.get();
  method proc pprint () { print "Hello "; a.print(); }
}

var b = joe a;
b.print();
b.pprint();
println$ b.get();
////////////////

Here, we used delegation to "emulate" inheriting methods.
The actual interface B[T] is and extension.

Note that with this technique "overriding" a base class method
is a non-sequitur i.e. its a nonsense idea, since we can either
delegate explicitly as shown, or, we can define a new method,
which is equivalent to inheriting the method or overriding it.

Note also we have inherited from an object NOT a class.
Its an actual, separately allocated object which we might
copy or not (if the factory function use "var" we could 
guarantee a copy by eager evaluation). However it
doesn't matter two hoots or more because we have
only copied the record of closures NOT the state ..

Felix "objects" are more dynamic and more object like than
those in C++ or Java. Ttechnically .. the state associated
with an object is separate from the object! 

The vtable, if you like, is the actual object and is linked  
to the state, in C++ its the other way around: 
the state is linked to the vtable.


So what if you wanted to encapsulate the state? So it couldn't perhaps
be externally modified?

Easy!!

object hilda[T] (lst:list[T]) implements B[T] = {
  var a = fred lst;
  ...
}

Thats really cool (hey I only just thought of that!)
Now the base state object is a local variable of the factory
function as so is hidden: the state is protected now.

So what I think we need to "fool"** Java programmers into thinking we
have OO is a simple syntax for method delegation. Any suggestions
how to encode the above technique in a slightly nicer syntax?

** Whilst I usually derive Java and OO I am not meaning to do so 
in this case. Heck, I implemented objects and interfaces "a la Java"
just for fun to show how powerful Felix was .. I didn't expect it would
be all that useful, just a curiosity.

But now, I'm using it extensively for implementing plugins
and breaking the webserver and documentation tools down into
small separately compiled units.

In fact, I was thinking "why not add state to classes" which would
give us Ocaml modules (by this I mean to allow value parameters
to classes, such as "hash function" is a parameter to Hashtables).
But actually .. objects subsume this .. I just didn't realise they
could be polymorphic :)


--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to