Difference between revisions of "Java integration planning"

From IokeWiki
Jump to: navigation, search
Line 23: Line 23:
 
x bar((short) 123, (org:something:Interface)anObj)
 
x bar((short) 123, (org:something:Interface)anObj)
 
</source>
 
</source>
 +
 +
== Class methods ==
 +
 +
Methods that belong to the Java class java.lang.Class are treated a bit differently from the way other Java methods work. The reason for this is a bit intricate to explain, but basically comes down to the dichotomy between the way mimicking works in Ioke, and the way inheritance works in Java.
 +
 +
The easiest way to explain this might be to take a look at java.util.ArrayList. In Java the hierarchy looks like this (somewhat simplified)
 +
 +
<pre>
 +
instance >> ArrayList -> AbstractList -> AbstractCollection -> Object
 +
                      -> Serializable
 +
                      -> Cloneable
 +
                      -> Iterable
 +
                      -> Collection
 +
                      -> List
 +
                      -> RandomAccess
 +
</pre>
 +
 +
Note here that all capitalized words are names of classes. The Class itself is not part of the inheritance structure, because Java has two namespaces. In Ioke, the above hierarchy basically ends up looking like this:
 +
 +
<pre>
 +
instance -> ArrayList -> Class
 +
                      -> AbstractList -> Class
 +
                                      -> AbstractCollection -> Class
 +
                                                            -> Object -> Class
 +
                      -> Serializable -> Class
 +
                      -> Cloneable -> Class
 +
                      -> Iterable -> Class
 +
                      -> Collection -> Class
 +
                      -> List -> Class
 +
                      -> RandomAccess -> Class
 +
</pre>
 +
 +
This is slightly simplified too, but the basic idea is that Class has to be part of the same inheritance chain, because there only exists one namespace in Ioke.
 +
 +
And that is why there is a need to have these methods named differently. So, for example, say that you want to invoke the <code>toString</code> method on <code>java:util:HashMap</code>. To do that, you do it like this: <code>java:util:HashMap class:toString</code>. Note that you can actually use the <code>class:</code> methods on instances of a class too. The result will be the same as if the receiver had been that class instead of the instance.

Revision as of 20:42, 10 February 2009

This information is for Ioke E+

Java classes can be accessed by their full name, separated with colon instead of dots:

h = java:util:HashMap new

You can invoke methods as expected, although the rules for this are a bit intricate. Specifically, the rules are that when calling methods that take any kind of primitive arguments (most of the types in java.lang), these values will be unwrapped. That means for a method taking an int, you can send in a wrapped Java Integer, but you can also send in an Ioke Number Rational, and it will be unwrapped correctly. You can send Text and Symbol to methods taking Strings.

If you send in data to a method taking Object, you will not get the expected result in all cases. Specifically, in these cases Ioke will unwrap wrapped Java objects, but will not coerce the primitive types. So doing h put("foo", "bar") will not coerce Text into java.util.String. However, if you have a java.lang.String that has been modified from the Ioke side - adding cells for example, then that String will be unwrapped before sent to the method call.

The return value from a Java invocation will never be modified, except that null will always be changed into nil. This means that if you wrap a Java object, do some modifications to it, and then let it go through a Java call, what you will get back is probably not the wrapped version of that object. Of course, later on if you try to call a method on the object, you will still use the wrapped data.

The general rule for overloading is that overloaded methods will be sorted from the most specific to the most general.

Casts

Ioke Java integration supporting casts in the same way as Java does. The main difference is that the cast will be evaluated at runtime, not compile time. The name to cast to will be unevaluated. Valid names are either Object, String, any Java class name where dots are separated with colons, and a few special ones to handle primitive values. These names are int, integer, long, short, char, character, float, double and boolean. Casting looks like this:

x = Foo new
x bar((short) 123, (org:something:Interface)anObj)

Class methods

Methods that belong to the Java class java.lang.Class are treated a bit differently from the way other Java methods work. The reason for this is a bit intricate to explain, but basically comes down to the dichotomy between the way mimicking works in Ioke, and the way inheritance works in Java.

The easiest way to explain this might be to take a look at java.util.ArrayList. In Java the hierarchy looks like this (somewhat simplified)

instance >> ArrayList -> AbstractList -> AbstractCollection -> Object
                      -> Serializable
                      -> Cloneable
                      -> Iterable
                      -> Collection
                      -> List
                      -> RandomAccess

Note here that all capitalized words are names of classes. The Class itself is not part of the inheritance structure, because Java has two namespaces. In Ioke, the above hierarchy basically ends up looking like this:

instance -> ArrayList -> Class
                      -> AbstractList -> Class
                                      -> AbstractCollection -> Class
                                                            -> Object -> Class
                      -> Serializable -> Class
                      -> Cloneable -> Class
                      -> Iterable -> Class
                      -> Collection -> Class
                      -> List -> Class
                      -> RandomAccess -> Class

This is slightly simplified too, but the basic idea is that Class has to be part of the same inheritance chain, because there only exists one namespace in Ioke.

And that is why there is a need to have these methods named differently. So, for example, say that you want to invoke the toString method on java:util:HashMap. To do that, you do it like this: java:util:HashMap class:toString. Note that you can actually use the class: methods on instances of a class too. The result will be the same as if the receiver had been that class instead of the instance.