Libraries - Create Object

Implemented as of Version2015.r1

A Objekt-Library is an instantiable class in the form of a library. It adds a new object data type, which must be constructed/instantiated before you can use it. The built-in Luna type „memoryblock“ is an example such an object data type. „String“ is also an object data type that is derived from the object „MemoryBlock“.

The library object, depending on the design such as a memoryblock provide corresponding functions that can be applied to the object. Each object has its private memory after construction. Objects can thus repeatedly exist at the same time in memory. The number of parallel instances is limited only by the available memory.

The object-data is stored in a memoryblock, so the object-variable is nil as long as it was not created/initialized. A object can be so constructed during the program period and destroyed.

The formerly internal object „Graphics“ was outsourced to a library object. It is also a example for a library object implementation. Furthermore, you will find another example xstring.object, which represents a separate string object in the folder /Library/Example/

In contrast to an interface or module, no nested syntax can be defined by keywords in an object. The syntax is derived from the functionality of the object and may additionally implemented further objects and their functions.

In a library object, there is only a root-level in which the actual functions (methods) are accessible by the Luna developers.

In the folder Constructors are the necessary object handlers (basis functions) they must have each object, so that the compiler can create the object, destroy, assign or embed it in expressions.

For the use of the object in expressions with operators - eg „+“, „-“, „and“, the functionality „Operator Overloading“ is supported. This optional handlers (basis functions) you can create for operators allows you - in example - to made a addition with your object or similar.

#library "Library/Graphics.object"
'[...]
dim g as Graphics
 
g = new Graphics(64,32)
if g<>nil then
  g.Font=font.Addr		'set font
  g.Text("Hello",1,1)
end if
 
'[...]
 
#includeData font,"fonts\vdi6x8.lf"

See also example library xstring.object in folder /Library/Example/ . The example library is stocked with corresponding descriptions. When creating e.g. a method or constructor, a short explanation is automatically available as comment.

Code-Example:
xstring-example.luna
#library "Library/Example/xstring.object"
 
avr.device = atmega328p
avr.clock = 20000000
avr.stack = 96
 
uart.baud = 19200			' Baudrate
uart.recv.enable			' Senden aktivieren
uart.send.enable			' Empfangen aktivieren
 
dim value as word
dim a,b,r as xstring
 
print 12
wait 1
 
a = new xstring("This is")
b = new xstring(" just ")
 
'the address of the objects data blocks (memoryblock) 
print "a.Ptr = 0x";hex(a.Ptr)
print "b.Ptr = 0x";hex(b.Ptr)
 
'the content of the objects
print "a = ";34;a;34
print "b = ";34;b;34
 
r = a + b + "amazing!"
 
print "r.Ptr = 0x";hex(r.Ptr)
print "r = ";34;r;34
 
'calling a function of the object
print "r.reverse = ";34;r.reverse;34
 
'calling a function who returns a object
print "return value of function 'test()' = ";34;test();34
 
'destroy a object
r = nil
 
 
halt()
 
'object as return value
function test() as xstring
  return new xstring("test")
endfunc