Reference types in the meso scheme
Class types in the meso: scheme are just like class types in Java. Each can inherit from a single parent class and implement multiple interface types. All meso: class types directly or indirectly inherit from the meso:meso.lang.Object class. A class may be an abstract type if at least one of its methods is abstract.
More interestingly, a meso: class can implement interface types from other schemes. For example, this meso:foo.Task class implements the java:java.lang.Runnable interface type:
namespace meso:foo;
import java:java.lang.*;
public class Task implements Runnable {
private string name = "";
public Task(string name) {
this.name = name;
}
// implements the Runnable.run() method
public void run() {
System.out.println( "Hello, " + name + "!" );
}
// an additional method in Task
public string getName() {
return name;
}
}
The instance of this meso: class can be used in a place that takes the Java java.lang.Runnable type argument. For example, the following Meso program passes the instance to java:java.lang.Thread:
namespace meso:foo;
import java:java.lang.*;
public class Bar {
public static void main(string[] args) {
// Task does not need to be imported since it is in the same namespace
Task task = new Task( "John Smith" );
// pass the meso instance to Java
Thread t = new Thread( task );
t.start();
// task.getName() is only availabe in the meso scheme
System.out.println( "Task name: " + task.getName() );
}
}
Run this program and you will see:
stefan@macpro:~/test$ java -jar meso.jar meso:foo.Bar Task name: John Smith Hello, John Smith! stefan@macpro:~/test$ _
Notice that the getName() method is not visible to Java programs even though it is declared as public. It belongs to the meso:foo.Bar class, which is in the meso: scheme. There is no corresponding foo.Bar class in Java. From the Thread class in Java, all it sees is a mysterious object that implements the java.lang.Runnable interface. This gives the object an additional layer of polymorphism, allowing it to respond to different set of interfaces in different scheme contexts. Internally, we use Java's dynamic proxy mechanism to implement this feature.
A meso: class can also implement remote imop: interfaces. We will discuss this in a latter section.
Interface types in the meso: scheme can extend multiple parent interfaces from varios type schemes.
For example, this is an interface type named meso:foo.INamedResource:
namespace meso:foo;
public interface INamedResource {
string getName();
}
This meso:foo.ITask interface extends from both a meso: and a java: interfaces:
namespace meso:foo;
import java:java.lang.Runnable;
public interface ITask extends INamedResource, Runnable {
boolean hasStarted();
}
This meso:foo.Task class implements the meso:foo.ITask interface, so it must implement all methods declared in and inherited by the meso:foo.ITask interface:
namespace meso:foo;
import java:java.lang.System;
public class Task implements ITask {
private string name = "";
private boolean started = false;
public Task(string name) {
this.name = name;
}
// declared in java's Runnable interface
public void run() {
started = true;
System.out.println( "Hello, " + name + "!" );
}
// declared in meso's INamedResource interface
public string getName() {
return name;
}
// declared in meso's ITask interface
public boolean hasStarted() {
return started;
}
}
An object type is a special class that owns a singleton object instance. The instance cannot be created with a new expression; instead, it is instantiated when the type is first referenced in the runtime, and lives indefinitely until the runtime exits. The UTL of an object type can be used as a R-value in an expression directly to represent the singleton instance.
An object type can inherit from one parent class type and implement multiple interface types, just like a class. However, object types cannot have constructors that takes input arguments. They can not be further inherited either.
The object type below implements the same java:java.lang.Runnable interface as the class type above:
namespace meso:foo;
import java:java.lang.*;
public object Task implements Runnable {
// only the default constructor is allowed
public Task() {
}
// implements the Runnable.run() method
public void run() {
System.out.println("Hello!");
}
}
The object type's name can be used as a R-value directly, which refers to the singleton instance:
namespace meso:foo;
import java:java.lang.Thread;
public class Bar {
public static void main(string[] args) {
Thread t = new Thread( Task ); // uses Task as a R-value
t.start();
}
}
Run this program and you will see:
stefan@macpro:~/test$ java -jar meso.jar meso:foo.Bar Hello! stefan@macpro:~/test$ _
