Instantiating objects in the REPL
GendL includes a collection of object definitions which you can use directly or customise/enhance to suit particular needs.
To work with instances of these objects you may choose to instantiate an object in the REPL. For example, to instantiate a GendL object of type box in the REPL, we call GendL's general-purpose make-object constructor function, which yields a freshly instantiated object which we assign to a symbol (my-box) in the below example), using the Lisp special operator setq. The make-object function takes an object name as its required argument, followed by a "spliced-in" list of alternating slot-names and corresponding desired initial values. This type of list which is made up from alternating key-value pairs is called a plist. You will learn more about plists later. In the example below we are instantiating a wireframe volumetric box and initializing its length, width and height slots to 2, 3, and 4, respectively.
Using the macro the-object, you can then "send a message" to this object, which yields the current value of the requested slot.
GDL-USER> (setq my-box (make-object 'box :length 2 :width 3 :height 4))#<BOX #x210348222D>
GDL-USER> (the-object my-box length)2
GDL-USER> (the-object my-box width)3
GDL-USER> (the-object my-box height)4
Note that setq is a standard CL special operator which takes one or more symbol-value pairs, assigns the respective values to the symbols, and finally returns the last value.
GDL-USER> (setq a 1)1
GDL-USER> (setq b 2 c "three" d nil)NIL
As with most built-in primitives, The GendL box supports a number of built-in messages, for example volume. To send the volume message to our my-box instance and get its value returned, we will need to use a reference chain. A reference chain consists of a reference operator such as the-object, followed by an expression which is expected to return an object instance (the example below this expression is simply that my-box symbol).
GDL-USER> (the-object my-box volume)24
You can use the abbreviation theo rather than the-object. It does the same thing with less typing.
GDL-USER> (theo my-box volume)24
As an added convenience, you can assign the object to the special variable self rather than to an arbitrarily-named variable. If you take this approach, then rather than using theo or the-object to refer to an object expression explicitly when sending a message, you now have the option of using the GendL macro the to send a message to the object, and automatically self will be assumed as the object to receive the message.
In the following REPL interaction, the last three expressions are equivalent with each other:
GDL-USER> (setq self (make-object 'box :length 2 :width 3 :height 4))#<BOX #x210348242D>
GDL-USER> (the volume)24
GDL-USER> (the-object self volume)24
GDL-USER> (theo self volume)24
While self is a normal symbol, it has a special meaning within the context of GendL. It is fine to use self in the REPL, you should never bind or set/modify self within the body of a GendL object definition (created using the define-object macro)
As yet an additional convenience, you can use the built-in GendL function make-self to instantiate an object and set it to the toplevel self, all in one fell swoop:
GDL-USER> (make-self 'box :length 1 :width 2 :height 3)#<BOX #x210348242D>
GDL-USER> (the volume)6
GDL-USER> (the-object self volume)6
GDL-USER> (theo self volume)6
If you'd like to create a named "blueprint" of an object instance, e.g. for being able to instantiate customized boxes without needing to feed in customized slot values, then you may use GendL's define-object macro. In the example below we are defining a new object my-box, which is effectively a customisation of the default Gendl box object, by specifying box as part of its mixin list. More about define-object later.
(define-object my-box-1 (box):input-slots((length 2)(width 3)(height 4)))
After this object definition has been compiled & loaded into the running system (in Emacs/Slime this can be done with the cursor in the object and pressing C-c C-c), then you can instantiate it in the REPL. Note that because the new object my-box specifies default values for the :input-slots , you do not need to feed them in when making instances of my-box in the REPL
GDL-USER> (setq self (make-object 'my-box-1))#<BOX #x210344342D>
GDL-USER> (the volume)24
However, you continue to have the ability to feed in alternative values of :input-slots for my-box
GDL-USER> (setq self (make-object 'my-box-1 :length 8))#<BOX #x210478242D>
GDL-USER> (the volume)96