Anvil | Smiths Smiths | Register Register | Login Login |
Search:
Show links Show tools Show tree | Previous document Next document | njet.org > Anvil > Documentation > Language reference > Classes

Classes

Everything inside Anvil is an object -- even integers and floats. An object is a binding between the class and instance.

When an object has been created, or instantiated, it has been given an identity: it remains in a fixed position in memory. Using the addressOf() function, the address of an object can be retrieved.

The class of an object is retrieved using classof obj or meta-attribute obj.class. Both of these expressions return an object of type anvil.runtime.Type, which can be used to investigate the contents of the class.

Some of the objects are said to be immutable, meaning that their contents cannot be changed after they are created. Immutable objects include integers, floats, strings, patterns, and tuples.

Most of the objects are said to be mutable, meaning that their contents can be changed during execution. User-defined classes are a good example, although they can also be coded in a manner that would make them immutable.

Once an object has been created, it is never explictily destroyed. However, when the Java virtual machine decides that the object is no longer reachable (by means of static, or stack references), it becomes eligible for garbage collection: the object is destroyed, and its associated memory address is released.

Every class belongs to a module. A module is a collection of classes and functions. There is only one instance of each module.

Classes and modules come in three different flavors. All of them are accessed a little bit differently, but they behave similarly.

Anvil script modules and classes

These are the ones written with Anvil scripts (and templates). Their base class is always anvil.lang.object. There is a limitation that these classes cannot extend native or reflected classes.

There are number of callbacks methods starting with underscore (_) with which the class' response to different operations may be modified. See Class customization.

Native library modules and classes

These are written in Java to be an integral part of Anvil. Special notation is used in order to obtain a high level of integration. Anvil library classes are so-called wrappers, whose function is to expose the contents and functionality of implementing class(es) in format Anvil can understand. Their base class is always anvil.lang.object.

Reflected Java packages and classes

Any Java package and class can be accessed from Anvil. (Unlike modules, Java packages are not aware of their classes until these classes are first accessed.) This is possibly due to Java's reflection facility. While its performance does not quite match that of Anvil native libraries, it is a useful feature to have available. The base class of these is always java.lang.Object.

Contributes notes:
Add a note
What's new | Anvil