Don't be fooled by its brevity. This unique kOS library enables object oriented programming in KerboScript and eliminates the need for any globals whatsoever. This is largely accomplished by combining the power of KerboScript's Lexicon and KOSDelegate structures, nesting functions inside of functions, Recursion, and Anonymous functions.
Table of Contents
See INSTALL.
A program creates a Lexicon
and passes it as an argument to the oolib
script. This Lexicon
will be used to encapsulate all namespaces, classes, objects, and methods while the program is running.
LOCAL nsr IS Lexicon(). // 1
RUNONCEPATH("/oolib/oolib", nsr). // 2
- The
Lexicon
should be empty and should not be global. - Run the
oolib
script and pass theLexicon
as the argument.
After oolib has been initialized, other scripts may be given access to it when they are run by passing the Lexicon
to them as an argument:
// In the calling script
RUNONCEPATH("script_being_run", nsr).
The script requiring oolib should accept the Lexicon
as a non-optional argument.
// In the called script
PARAMETER nsr.
The capabilities of oolib are many and are still being discovered.
The oolib
library script accepts a Lexicon
as an input/output argument, and immediately clears its contents. This is why it is best to provide a newly created empty one. This Lexicon
will contain all namespaces, classes, objects, and methods.
PARAMETER nsr.
nsr:CLEAR.
NOTE: The remainder of this section assumes familiarity with class-based object-oriented programing terminology.
The oolib library then declares what is technically a Lexicon
but is logically the root of the oolib class hierarchy. Every class has Object as a superclass. All objects implement the methods of this class (once there are any).
LOCAL Object IS Lexicon( // 1
"new", { // 2
PARAMETER this IS Lexicon(). // 3
this:CLEAR. // 4
RETURN this. // 5
}). // 6
Object
is actually theObject class
.Object:new
is a class method; a constructor.Object:new()
will create a new object instance.- The
Lexicon
is emptied to make room for the object. - Returns the
Lexicon
of a newly created object instance. - Closes the
new
"anonymous" method, and then theObject
variable declaration.
A Lexicon
is actually an associative array, also known as a map, symbol table, or dictionary. The oolib Map
is essentially a Lexicon
, but implemented as a subclass of Object
. It is used by oolib to implement namespaces and a registry of namespaces.
LOCAL Map IS Lexicon( // 1
"new", { // 2
PARAMETER content is Lexicon(), // 3, 4
this IS Object:new(). // 5
SET this["put"] TO { // 6
PARAMETER key, value.
SET content[key] TO value.
RETURN this.}.
SET this["get"] TO { // 7
PARAMETER key.
RETURN content[key].}.
SET this["del"] TO { // 8
PARAMETER key.
content:REMOVE(k).
RETURN this.}.
SET this["all"] TO {RETURN content.}. // 9
RETURN this. // 10
}).
Map
is actually theMap class
.Map:new
is a class method; a constructor.Map:new()
without arguments will construct and return an emptyMap
.Map:new(Lexicon)
will construct and return a pre-populatedMap
.this IS Object:new()
calls this subclass' superclass,Object
, constructor.Map:put(key,value)
adds the key/value pair to theMap
, then return theMap
. This and the other instance methods are defined here as anonymous functions, but associated via theLexicon
with a name.Map:get(key)
returns the value associated with the given key.Map:del(key)
removes the key and its associated value from theMap
, returning theMap
.Map:all()
returns aLexicon
containing the content of theMap
.- Closes the
new
"anonymous" method, and then theMap
variable declaration.
The oolib is added to the name space registry. For this reason, the code sample is provided below, but not further explained.
LOCAL library IS Map:new(). // 1
library:put("Object", Object). // 2
library:put("Map", Map).
LOCAL namespace IS Map:new(Lexicon(), Object:new(nsr)). // 3
namespace:put("oo", library). // 4
- The oolib library namespace is created, which will be put into the nsr (name space registry).
- Objects are added to the namespace.
- Magic occurs. This is where the nsr is created, and it only happens once. Here.
- The oolib library namespace is the first namespace to go into the nsr.
TODO
This document is part of the kOS Object-Oriented Library documentation.
Copyright © 2020 Steve Meacham and contributors
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License v1.3 or (at your option) later published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included.