Skip to content

Pointers

Richard Baltrusch edited this page Mar 9, 2021 · 3 revisions

Pointers

Objectbatch uses a pointer-like variable naming system to avoid overwriting the contents of instance attributes for instantiated objects. For example, we may instantiate a new object obj of type object using the following syntax:

call new object obj construct

This call initializes the variable obj to a unique random ID which will act as our pointer to the newly created object.

Pointer syntax

The pointer-type variable names start with a dollar sign ($) followed by a lot of numbers. For example, the construct call above may set the value of obj to something like $090320211912198228855.

Object attributes

This pointer is then used to store the attributes of an object. For example, if we define attributes a and b for our object obj:

set %obj%.a=0
set %obj%.b=0

we are really declaring two variables in the following format:

  • pointer.a (e.g. $090320211912198228855.a)
  • pointer.b (e.g. $090320211912198228855.b)

This enables us to contain different values in attributes a and b for different objects obj1 and obj2 (if both of these objects contain a different pointer).

Objects from scratch using pointers

We may use pointers to define objects and dynamically add attributes (but not methods!) to them without any class definition. For example, to define a custom object with attributes a and b, we may write:

call get_new_ptr myPtr
set %myPtr%.a=1
set %myPtr%.b=2

We can also use this dynamic attribute definition for dependency injection using inheritance by defining the __super__ variable outside of a class definition.

For example, to inherit the class constructor and all instance attributes defined in it from a class myClass2 using our pointer myPtr defined above, we may write:

set __super__=myClass2
set self=%myPtr%
call super self construct

More information

More information on the provided pointer keywords can be found on the home page.

Clone this wiki locally