Defining Local Functions in Structures
You can define local functions in structures, providing a convenient mechanism for
packaging a set of related functions and avoiding possible name conflicts when many
global functions are defined, possibly by several separate scripts.
FOR EXAMPLE,
|
struct foo
(
a, b, c,
fn baz n = sqrt (n + 1),
fn bar x y = print (x ^ 2 + y ^ 2)
)
|
This defines a structure definition foo with three elements named a , b , and c , and two local functions: bar and baz . You can access these functions as properties of either the structure definition
value itself or an instance of the foo structure:
|
p = foo.baz q -- access as property of the structure definition value
foo.bar x y
f = foo 2 3 4
f.bar x y -- access as property of the structure instance
|
Local functions declared in a structure definition can make references to the member
data variables defined in the structure, such that when that member function in some
instance of the structure is called, the references access the members of that instance.
This allows you to initialize and maintain data structures for the structure functions
within the structure itself. An example of including functions that use members of
the structure is shown in the following script.
SCRIPT:
|
struct RandVals
(
RndVals=#(), ptr, eedVal,
fn generateRV num fromVal toVal =
(
if seedVal != undefined do seed seedVal
RndVals=for i=1 to num collect random fromVal toVal
ptr=1
RndVals
),
fn deleteRv =
(
RndVals=#()
ptr=undefined
undefined
),
fn getNextRV =
(
if Nvals == 0 do return undefined
val=RndVals[ptr]
ptr += 1
if ptr >RndVals.count do ptr=1
val
),
fn sortRV =(sort RndVals),
fn setSeed val = (SeedVal = val),
fn setPointer val =
(
if val > 0 and val <= RndVals.count then
(ptr=val;true)
else
false
)
)
MyRandomVals= RandVals()
MyRandomVals.setSeed 12345
MyRandomVals.generateRV 10 0 100
MyRandomVals.getNextRV()
MyRandomVals.sortRV()
MyRandomVals.setPointer 1
MyRandomVals.getNextRV()
MyRandomVals.deleteRv()
|
OUTPUT:
|
#Struct:RandVals( -- result of struct def lines 1 to 29
RndVals:<data>; Public,
sortRV:<fn>; Public,
ptr:<data>; Public,
seedVal:<data>; Public,
generateRV:<fn>; Public,
deleteRv:<fn>; Public,
getNextRV:<fn>; Public,
setSeed:<fn>; Public,
setPointer:<fn>; Public)
(RandVals RndVals:#() ptr:undefined seedVal:undefined) -- result line 30
12345 -- result line 31
#(23, 59, 79, 68, 17, 72, 84, 16, 89, 72) -- result line 32
23 -- result line 33
#(16, 17, 23, 59, 68, 72, 72, 79, 84, 89) -- result line 34
true -- result line 35
16 -- result line 36
undefined -- result line 37
|
In this script, the structure has three data members and six function members. Note
the references to the structure data members in the generateRV() function. In line 30, an instance of the RndVals structure is created and stored in variable MyRandomVals . In line 31, the setSeed() function in the structure instance is called, which initializes the random seed variable
by storing the specified value in data member seedVal in the structure instance. In line 32, the generateRV() function in the structure instance is called, which initializes the random number
using the seed value specified by setSeed() , sets up and stores an array of random numbers in data member RndVals , updates data member ptr , and then returns the random number array.
Local functions defined in structures are not stored in every instance of the structure.
The local function is stored once in the structure definition and references to them
in the instances refer to their definitions in the structure definition.