Allocating objects

You can create a instance of a class-type by “calling” the class-type as a function:

(<java.util.Vector> 20)

I.e. if you call a type as if were a function, then the type is coerced to an instance-constructor function.

Often the most convenient way to allocate a new object is to use the namespace-colon syntax discussed in the previous section. For example:

(java.util.Vector:new 20)

A third slightly more verbose option is to use the make builtin function.

Function: make type args ...

Constructs a new object instance of the specified type, which must be either a java.lang.Class or a <gnu.bytecode.ClassType>.

The args ... are passed to the constructor of the class type. If there is no applicable constructor, and the args ... consist of a set of (keyword,value)-pairs, then the default constructor is called, and each (keyword,value)-pair is used to set the correspdong slot of the result, as if by: (slot-set! result keyword value).

For example, the following are all equivalent:

(set! p (make <java.awt.Point> 3 4))

(set! p (make <java.awt.Point> y: 4 x: 3))

(set! p (make <java.awt.Point>))
(slot-set! p 'x 3)
(set! (slot-ref p 'y) 4)

All of these will usually be optimized to efficient bytecode, assuming Kawa knows at compile-time which class you're using.