Skip to content
Sébastien Doeraene edited this page Apr 12, 2012 · 8 revisions

Using interfaces

In order to abstract away actual data types, the object model supports interfaces. An interface defines a set of methods that are more or less semantically related.

Contrary to interfaces in most programming languages, which are implemented only by certain data types, interfaces in the VM object model are conceptually implemented by all data types. It's just that interface methods have a default implementation (for data types that do not provide a specific one). For most operation, this default implementation raises a type error exception.

In a previous page, we used the following trivial code:

UnstableNode node = UnstableNode::build<SmallInt>(vm, 5);
RichNode richNode = node;
cout << richNode.as<SmallInt>().value() << endl;

Let us now update this code to use the IntegerValue interface:

UnstableNode node = UnstableNode::build<SmallInt>(vm, 5);

IntegerValue interface = node; // with an implicit conversion to RichNode
nativeint value = 0;
BuiltinResult result = interface.intValue(vm, &value);

if (result.isProceed()) {
  cout << value << endl;
} else {
  // wait requested, or exception thrown, see later
  // but usually this information must fall through and reach the caller
  return result;
}

// Everything went well
return BuiltinResult::proceed();

Now, we have abstracted away our code from the actual data type which was SmallInt. The method IntegerValue::intValue() takes care of the dispatching for us.

However, now this can fail, either because the node referred to an unbound variable, or because it referred to a boolean or any other data type. In these cases, the Oz semantics require that we respectively wait until the variable is bound, or raise an exception. This is encoded into the BuiltinResult value that is returned by intValue(). If result.isProceed() is true, that means that the call executed normally, and we can proceed to displaying the value. Otherwise, we need to forward the information to the caller. Eventually it will reach the emulator loop, which knows what to do with such things.