Creating and Extending Types
Creating and extending Java types: the key feature.
Perhaps one of the most important features of Evo is the ability to create and extend Java classes. You can use Java.create
for this.
Let's say we want to make our own Runnable implementation. Runnable is an interface, so it doesn't have its own constructor. That means we have to make one.
We'll create a PrintRunnable
. It takes in a Java String
, and we'll print that string once it is run.
Now, let's break this down. The first argument, [ Runnable ]
is a list of all types we're inheriting from: one class (optionally), and as many interfaces as we want.
Then, in the anonymous class we create, we define a property called [Java.constructor]
(This is a JS symbol, so it doesn't clutter the class property namespace.) We only have one constructor, and that constructor just takes in one argument. However, we can also do multiple constructors:
Our constructor is just as normal, except it takes in a Java String, not a JS String. Arguments aren't converted if they're parameters in a Java.create
class, although you can convert them manually using Java.fromJava
and Java.toJava
.
What if I want to add my own new method, though? You still can, you just have to specify types yourself. The last type is treated as the return type.
It should also be noted that you can just do this:
If you have nothing to inherit from.
On the other hand, if you want to create an interface, you can use the Java.impl
.
Last updated