Behaviors
Providing native behaviors for many Java types.
Consider the following: Java has an interface called Iterable
, which many types of Java collections implement. Now, if we found a way to make it follow the iterable protocol, we could easily iterate over any Java class in TypeScript.
This is where the Behavior API comes in. You define a predicate, and an apply as a behavior. Once that Java class is initialized somewhere, if it meets the predicate, it will be applied.
Keep in mind this is the TypeScript API. There will almost certainly also be a Java API.
Java.behavior({
predicate(object) {
return object instanceof Iterable;
}
apply(object) {
object[Symbol.iterator] = *function() {
let iterator = object.iterator();
while (iterator.hasNext()) {
yield iterator.next();
}
return this;
}
}
});
Three types of behaviors will be natively implemented:
Native
Iterable
support.
for (const el of javaIterable) {
console.log(el);
}
One-method interfaces will have a constructor to make them from lambdas.
let runnable = new Runnable(() => console.log("Hi!"));
Interfaces will have a constructor to turn objects into them. Each object will have its own method.
let runnable = new Runnable({
run() {
console.log('Hi!');
}
});
Last updated