ExpandCollapsePrev Next Index

+ 3.1 Object Coercions

Suppose we have

  interface employment {
    get_name: 1 -> string;
    get_job: 1 -> string;

then of course we can write a function:

  proc print_occupation (p: employment) {
  println$ p.get_name() + " is a " + p.get_job();

But how do we call this function on working_john? It has the wrong interface because it tells the age, and the anti-discrimination laws won't allow age to be considered in the work place. Here's how we chuck out the age: we use a coercion:

  include "./objects_01";
  print_occupation (working_john :>> employment);

The coercion is really simple: it throws out all but the required fields. If some fields are missing you get a compile time type error. Although a coercion makes a new object, the state space holding its private representation is not affected.

I hope you now note a very important strength of this system: In Felix interfaces can be designed after objects are implemented and any existing objects with sufficient fields can be coerced to that type.

Unlike conventional OO systems, objects and interfaces use structural typing based on a set of named method types. So you can design you abstractions later, without needing to invade class definitions to add new bases.