Howto:Start using vectors and hashes in Nasal: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
m (Switch to the {{forum link}} template for all forum links.)
(28 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Stub}}
{{Nasal Navigation}}
Objective: Learn more about vectors and hashes to see how you can simplify and generalize your code easily.


Here we look into how to '''start using vectors and hashes in Nasal''' to simplify and generalize code.  Vectors and hashes are two data structures that [[Nasal]] uses to store data.  Vectors are in some other programming languages called for example lists or one-dimensional arrays, and hashes are similar to C's structs.
In many cases repetitive code can be generalized and rewritten into vectors, hashes or helper functions using them.  Some of the advantages with rewriting repetitive code this way is that it helps maintenance by requiring less typing when modifying and/or when fixing bugs.
In addition to storing data hashes can "store" functions and be used as object classes.  Object classes can be created by adding a function into a hash, that will create a new hash when called, a so called ''constructor'' (sometimes abbreviated to ''ctor'' or ''c'tor'') usually named <code>'''new'''()</code>.
== Learn more about vectors ==
Imagine a piece of Nasal code working with 5 different waypoints. You would need to manage the data for each waypoint. A simple version using different variables for each data set might look like this:
Imagine a piece of Nasal code working with 5 different waypoints. You would need to manage the data for each waypoint. A simple version using different variables for each data set might look like this:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
      var wp1 = 0;
var wp1 = 0;
      var wp1alt = 0;
var wp1alt = 0;
      var wp1dist = 0;
var wp1dist = 0;
      var wp1angle = 0;
var wp1angle = 0;
      var wp1length = 0;
var wp1length = 0;
      var wp1id = "";
var wp1id = "";
      var wp1brg = 0;
var wp1brg = 0;


      var wp2 = 0;
var wp2 = 0;
      var wp2alt = 0;
var wp2alt = 0;
      var wp2dist = 0;
var wp2dist = 0;
      var wp2angle = 0;
var wp2angle = 0;
      var wp2length = 0;
var wp2length = 0;
      var wp2id = "";
var wp2id = "";
      var wp2brg = 0;
var wp2brg = 0;


      var wp3 = 0;
var wp3 = 0;
      var wp3alt = 0;
var wp3alt = 0;
      var wp3dist = 0;
var wp3dist = 0;
      var wp3angle = 0;
var wp3angle = 0;
      var wp3length = 0;
var wp3length = 0;
      var wp3id = "";
var wp3id = "";
      var wp3brg = 0;
var wp3brg = 0;


      var wp4 = 0;
var wp4 = 0;
      var wp4alt = 0;
var wp4alt = 0;
      var wp4dist = 0;
var wp4dist = 0;
      var wp4angle = 0;
var wp4angle = 0;
      var wp4length = 0;
var wp4length = 0;
      var wp4id = "";
var wp4id = "";
      var wp4brg = 0;
var wp4brg = 0;


      var wp5 = 0;
var wp5 = 0;
      var wp5alt = 0;
var wp5alt = 0;
      var wp5dist = 0;
var wp5dist = 0;
      var wp5angle = 0;
var wp5angle = 0;
      var wp5length = 0;
var wp5length = 0;
      var wp5id = "";
var wp5id = "";
      var wp5brg = 0;
var wp5brg = 0;
</syntaxhighlight>
</syntaxhighlight>


Line 49: Line 55:


Just imagine we'd need to support more than just 5 waypoints, like maybe 10, 20, 50 or maybe even 1000 waypoints. As you can see, this method is very inflexible, complicated, tedious and error-prone.
Just imagine we'd need to support more than just 5 waypoints, like maybe 10, 20, 50 or maybe even 1000 waypoints. As you can see, this method is very inflexible, complicated, tedious and error-prone.
Such code contains so called "code smells", another example would be code like this:
<syntaxhighlight lang="nasal">
var l0 = "";
var l1 = "";
var l2 = "";
var l3 = "";
var l4 = "";
var l5 = "";
var l6 = "";
var l7 = "";
var l8 = "";
var l9 = "";
var l10 = "";
var l11 = "";
var l12 = "";
var l13 = "";
var l14 = "";
var l15 = "";
var l16 = "";
var l17 = "";
var l18 = "";
var l19 = "";
var l20 = "";
var hist1 = "";
var hist2 = "";
var hist3 = "";
var hist4 = "";
var hist5 = "";
var hist6 = "";
IDENTl3 = "";
IDENTl4 = "";
IDENTr4 = "";
IDENTl5 = "";
IDENTr5 = "";
IDENTl8 = "";
setprop("/instrumentation/efb/display/line0-l", l0);
setprop("/instrumentation/efb/display/line1-l", l1);
setprop("/instrumentation/efb/display/line2-l", l2);
setprop("/instrumentation/efb/display/line3-l", l3);
setprop("/instrumentation/efb/display/line4-l", l4);
setprop("/instrumentation/efb/display/line5-l", l5);
setprop("/instrumentation/efb/display/line6-l", l6);
setprop("/instrumentation/efb/display/line7-l", l7);
setprop("/instrumentation/efb/display/line8-l", l8);
setprop("/instrumentation/efb/display/line9-l", l9);
setprop("/instrumentation/efb/display/line10-l", l10);
setprop("/instrumentation/efb/display/line11-l", l11);
setprop("/instrumentation/efb/display/line12-l", l12);
setprop("/instrumentation/efb/display/line13-l", l13);
setprop("/instrumentation/efb/display/line14-l", l14);
setprop("/instrumentation/efb/display/line15-l", l15);
setprop("/instrumentation/efb/display/line16-l", l16);
setprop("/instrumentation/efb/display/line17-l", l17);
setprop("/instrumentation/efb/display/line18-l", l18);
setprop("/instrumentation/efb/display/line19-l", l19);
setprop("/instrumentation/efb/display/line20-l", l20);
</syntaxhighlight>
Whenever you see such code, it should be obvious that using arrays/vectors would be a good idea.


So, it would be better to use a vector of waypoints instead. A vector is a list of things (variables) that can be easily accessed using an index into the vector. Pretty much like an array in C or C++, with the added advantage that the vector can be dynamically resized, e.g. using the setsize() library call. Consider the following example:
So, it would be better to use a vector of waypoints instead. A vector is a list of things (variables) that can be easily accessed using an index into the vector. Pretty much like an array in C or C++, with the added advantage that the vector can be dynamically resized, e.g. using the setsize() library call. Consider the following example:




<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
var waypoints = ["wp1", "wp2", "wp3", "wp4", "wp5"];
</syntaxhighlight>
</syntaxhighlight>


Line 63: Line 132:
The problem is, that this only gives us a list of single waypoints:
The problem is, that this only gives us a list of single waypoints:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
var waypoints = ["wp1", "wp2", "wp3", "wp4", "wp5"];
print( waypoints[0] ); # print wp1
print(waypoints[0]); # print wp1
print( waypoints[1] ); # print wp2
print(waypoints[1]); # print wp2
print( waypoints[2] ); # print wp3
print(waypoints[2]); # print wp3
print( waypoints[3] ); # print wp4
print(waypoints[3]); # print wp4
print( waypoints[4] ); # print wp5
print(waypoints[4]); # print wp5
</syntaxhighlight>
</syntaxhighlight>


To do this using a conventional for loop, you would use the size() function to get the size of the vector:
To do this using a conventional for loop, you would use the size() function to get the size of the vector:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
for(var index; index < size(waypoints); index=index+1) {
for(var index; index < size(waypoints); index=index+1) {
print(waypoints[index]);
    print(waypoints[index]);
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 83: Line 152:
The same thing can be accomplished using the forindex loop:
The same thing can be accomplished using the forindex loop:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
forindex(var index; waypoints) {
forindex(var index; waypoints) {
print(waypoints[index]);
    print(waypoints[index]);
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 92: Line 161:
Now, waypoints being a vector, we can also use a simple foreach loop, that works without a counter, to process each element easily:
Now, waypoints being a vector, we can also use a simple foreach loop, that works without a counter, to process each element easily:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
foreach(var wpt; waypoints) {
foreach(var wpt; waypoints) {
print(wpt);
    print(wpt);
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 101: Line 170:
What we really need to save all the other waypoint specific information is a new variable type that serves as the "container" for variables, so that we can save several variables for each waypoint.
What we really need to save all the other waypoint specific information is a new variable type that serves as the "container" for variables, so that we can save several variables for each waypoint.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var wp4 = 0; # waypoint number
var wp4 = 0;       # waypoint number
var wp4alt = 0; # waypoint altitude
var wp4alt = 0;   # waypoint altitude
var wp4dist = 0; # waypoint distance
var wp4dist = 0;   # waypoint distance
var wp4angle = 0; #waypoint angle
var wp4angle = 0; # waypoint angle
var wp4length = 0; # waypoint length
var wp4length = 0; # waypoint length
var wp4id = "";       # waypoint id
var wp4id = "";   # waypoint id
var wp4brg = 0; # waypoint bearing
var wp4brg = 0;   # waypoint bearing
</syntaxhighlight>
</syntaxhighlight>


Line 117: Line 186:
So we could be using a "main" vector to store all waypoints, while each field of the main vector is a different vector that contains all waypoint-specific data.
So we could be using a "main" vector to store all waypoints, while each field of the main vector is a different vector that contains all waypoint-specific data.


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var wp4 = 0; # waypoint number
var wp4 = 0;       # waypoint number
var wp4alt = 0; # waypoint altitude
var wp4alt = 0;   # waypoint altitude
var wp4dist = 0; # waypoint distance
var wp4dist = 0;   # waypoint distance
var wp4angle = 0; #waypoint angle
var wp4angle = 0; # waypoint angle
var wp4length = 0; # waypoint length
var wp4length = 0; # waypoint length
var wp4id = "";       # waypoint id
var wp4id = "";   # waypoint id
var wp4brg = 0; # waypoint bearing
var wp4brg = 0;   # waypoint bearing


var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg];
var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg];
Line 134: Line 203:
So, the very first vector element would be waypoints[0] and it would point to another vector (waypoint4), the elements of waypoint4 would be also available by index:
So, the very first vector element would be waypoints[0] and it would point to another vector (waypoint4), the elements of waypoint4 would be also available by index:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var wp4 = 1; # waypoint number
var wp4 = 1;         # waypoint number
var wp4alt = 1000; # waypoint altitude
var wp4alt = 1000;   # waypoint altitude
var wp4dist = 20.4; # waypoint distance
var wp4dist = 20.4; # waypoint distance
var wp4angle = 33.4; #waypoint angle
var wp4angle = 33.4; #waypoint angle
var wp4length = 12; # waypoint length
var wp4length = 12; # waypoint length
var wp4id = "none";       # waypoint id
var wp4id = "none"; # waypoint id
var wp4brg = 122; # waypoint bearing
var wp4brg = 122;   # waypoint bearing




Line 160: Line 229:
Obviously, you could add a bunch of other waypoints to the "waypoints" vector, too:
Obviously, you could add a bunch of other waypoints to the "waypoints" vector, too:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg];
var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg];
var waypoint5 = [wp5,wp5alt,wp5dist,wp5angle,wp5length,wp5id, wp5brg];
var waypoint5 = [wp5,wp5alt,wp5dist,wp5angle,wp5length,wp5id, wp5brg];
Line 168: Line 237:
The only problem with this approach is, that you'll need to set up all those wp variables - so there's a shorter version possible, by directly adding your data without using variables, i.e. "inline":
The only problem with this approach is, that you'll need to set up all those wp variables - so there's a shorter version possible, by directly adding your data without using variables, i.e. "inline":


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint4 = [1,1000,12,22,44,"none", 33];
var waypoint4 = [1,1000,12,22,44,"none", 33];
var waypoint5 = [2,1500,22,42,14,"none", 133];
var waypoint5 = [2,1500,22,42,14,"none", 133];
var waypoints = [waypoint4, waypoint5]
var waypoints = [waypoint4, waypoint5];
</syntaxhighlight>
</syntaxhighlight>


There's an even more succinct version possible. The next step would be to use embedded vectors directly:
There's an even more succinct version possible. The next step would be to use embedded vectors directly:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [
[1,1000,12,22,44,"none", 33],  
[2,1500,22,42,14,"none", 133]
];
</syntaxhighlight>
</syntaxhighlight>


Accessing such a vector with embedded (or nested) vectors is still simple:
This is just formatted for clarity, the following snippet would be equivalent, but not as readable to people new to vectors:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [ [1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133] ];
print(waypoints[0][0]) # prints 1
</syntaxhighlight>
print(waypoints[0][1]) # prints 1000
 
print(waypoints[1][0]) # prints 2
 
print(waypoints[1][1]) # prints 1500
Accessing such a vector with embedded (or nested) vectors is still as simple:
 
<syntaxhighlight lang="nasal">
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
print(waypoints[0][0]) # prints 1
print(waypoints[0][1]) # prints 1000
print(waypoints[1][0]) # prints 2
print(waypoints[1][1]) # prints 1500
</syntaxhighlight>
</syntaxhighlight>


The only issue here is that you'll need to keep vector ordering in mind, so that you can always access the right element number. This could be simplified by using some variables with telling names as the index for each vector:
The only issue here is that you'll need to keep vector ordering in mind, so that you can always access the right element number. This could be simplified by using some variables with telling names as the index for each vector:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
# these are used self-explanatory indices - so that you don't need to remember each field's position:
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
print(waypoints[0][ALTITUDE]) # prints 1
 
print(waypoints[0][DISTANCE]) # prints 1000
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
print(waypoints[1][ALTITUDE]) # prints 2
print(waypoints[0][ALTITUDE]) # prints 1
print(waypoints[1][DISTANCE]) # prints 1500
print(waypoints[0][DISTANCE]) # prints 1000
print(waypoints[1][ALTITUDE]) # prints 2
print(waypoints[1][DISTANCE]) # prints 1500
</syntaxhighlight>
</syntaxhighlight>


Now, this yields already fairly readable source code:
Now, this yields already fairly readable source code:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]


# print the altitude for each waypoint in the vector:
# print the altitude for each waypoint in the vector:
foreach(var waypoint; waypoints) {
foreach(var waypoint; waypoints) {
  print( waypoint[ALTITUDE] );
    print( waypoint[ALTITUDE] );
}
}
</syntaxhighlight>
</syntaxhighlight>


So, this would already be much better than our original version, because we can now have an arbitrary number of waypoints. New waypoints would need to be added to the waypoints vector by using the append() library function:
So, this would already be much better than our original version, because we can now have an arbitrary number of waypoints. New waypoints would need to be added to the waypoints vector by using the append() library function:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
print(waypoints[0][ALTITUDE]) # prints 1
print(waypoints[0][ALTITUDE]) # prints 1
print(waypoints[0][DISTANCE]) # prints 1000
print(waypoints[0][DISTANCE]) # prints 1000
print(waypoints[1][ALTITUDE]) # prints 2
print(waypoints[1][ALTITUDE]) # prints 2
print(waypoints[1][DISTANCE]) # prints 1500
print(waypoints[1][DISTANCE]) # prints 1500


append(waypoints, [3,3000,122,212,34,"none", 133] );
append(waypoints, [3,3000,122,212,34,"none", 133] );
</syntaxhighlight>
</syntaxhighlight>


Next, we could come up with some helper functions to deal with the vector details transparently:
Next, we could come up with some helper functions to deal with the vector details transparently, using the same method as before: self-explanatory indices, which spares us having to remember the purpose of each field:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]];


var get = func (n,what) {
var get = func (n,what) {
  return waypoints[n][what];
    return waypoints[n][what];
}
}


var set = func (n,what,value) {
var set = func (n,what,value) {
  waypoints[n][what]=value;
    waypoints[n][what]=value;
}
}
 
print( get(0, ALTITUDE)  ) # prints 1
print( get(0, DISTANCE)  ) # prints 1000
print( get(1, ALTITUDE)  ) # prints 2
print( get(1, DISTANCE)  ) # prints 1500


print( get(0, ALTITUDE)  ); # prints 1
print( get(0, DISTANCE)  ); # prints 1000
print( get(1, ALTITUDE)  ); # prints 2
print( get(1, DISTANCE)  ); # prints 1500
</syntaxhighlight>
</syntaxhighlight>


Line 252: Line 332:
You could even add some more high level helpers for each waypoint field:
You could even add some more high level helpers for each waypoint field:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]];


var get = func (n,what) {
var get = func (n,what) {
  return waypoints[n][what];
    return waypoints[n][what];
}
}


var set = func (n,what,value) {
var set = func (n,what,value) {
  waypoints[n][what]=value;
    waypoints[n][what]=value;
}
}


var get_alt = func(n) {
var get_alt = func(n) {
  return get(n,ALTITUDE);
    return get(n,ALTITUDE);
}
}


var get_dist = func(n) {
var get_dist = func(n) {
  return get(n,DISTANCE);
    return get(n,DISTANCE);
}
}


var get_angle = func(n) {
var get_angle = func(n) {
  return get(n,ANGLE);
    return get(n,ANGLE);
}
}


var get_length = func(n) {
var get_length = func(n) {
  return get(n,LENGTH);
    return get(n,LENGTH);
}
}


var get_id = func(n) {
var get_id = func(n) {
  return get(n,ID);
    return get(n,ID);
}
}


var get_brg = func(n) {
var get_brg = func(n) {
  return get(n,BEARING);
    return get(n,BEARING);
}
}
</syntaxhighlight>
</syntaxhighlight>


Line 300: Line 380:
Consider the following empty hash:
Consider the following empty hash:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {};
var waypoint = {};
</syntaxhighlight>
</syntaxhighlight>


Next, we are going to add some fields to our new variable "container":
Next, we are going to add some fields to our new variable "container". Each new field can be assigned an initial value, this is done by treating the field as a value/key pair, with a colon (:) separating both. And with multiple fields being separated by a comma (formatting only added for clarity):
 
<syntaxhighlight lang="nasal">
# declare a new hash named waypoint
var waypoint = {
# add a few fields and assign a value to each field:
number:0,
altitude:0,
distance:0,
angle:0,
length:0,
ID:0,
bearing:0
};
</syntaxhighlight>


<syntaxhighlight lang="php">
which is equivalent to this, more succinct, version:
<syntaxhighlight lang="nasal">
var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0};
var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0};
</syntaxhighlight>
</syntaxhighlight>
Line 323: Line 418:
Now, to access any of these fields, we would use the "dot notation" by first specifying the name of the enclosing context (which is really just a fancy word for the name of the hash) and the name of the field we are interested in. You could read this as: LOCATION.FIELD (i.e. get FIELD out of location).
Now, to access any of these fields, we would use the "dot notation" by first specifying the name of the enclosing context (which is really just a fancy word for the name of the hash) and the name of the field we are interested in. You could read this as: LOCATION.FIELD (i.e. get FIELD out of location).


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0};
var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0};
print ( waypoint.number );
print ( waypoint.number );
Line 338: Line 433:
Now, to make this is a little more interesting and to show what's happening, we are going to change the value of each field:
Now, to make this is a little more interesting and to show what's happening, we are going to change the value of each field:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
print ( waypoint.number ); # prints 1
print ( waypoint.number ); # prints 1
Line 351: Line 446:
So, you could obviously create several different versions of this hash to store your waypoint data:
So, you could obviously create several different versions of this hash to store your waypoint data:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint1 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint1 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint2 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint2 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
Line 363: Line 458:
But the really cool thing comes next:
But the really cool thing comes next:


As you may have noticed, the code for each waypoint is 100% identical - so we could just as well tell the Nasal engine to use an existing hash as a TEMPLATE for a new object. This is accomplished using the "parents" keyword:
As you may have noticed, the code (internal structure of fields) for each waypoint is 100% identical - so we could just as well tell the Nasal engine to use an existing hash as a TEMPLATE for a new object instead of having to replicate the hash over and over again. This is accomplished using the "parents" keyword:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var Position3D = {x:1.00, y:2.00, z:3.00};
var Position3D = {x:1.00, y:2.00, z:3.00};
var p1 = { parents: [Position3D] };
var p1 = { parents: [Position3D] };
Line 378: Line 473:
This means, that the previously posted code could be easily abbreviated and written like this:
This means, that the previously posted code could be easily abbreviated and written like this:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint1 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint1 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint2 = {parents:[waypoint1] };
var waypoint2 = {parents:[waypoint1] };
Line 388: Line 483:
You could even introduce a new helper function to create new objects, let's call it new_waypoint:
You could even introduce a new helper function to create new objects, let's call it new_waypoint:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var new_waypoint = func {return {parents:[waypoint] };}
var new_waypoint = func() {
return { parents:[waypoint] };
}


var waypoint1 = new_waypoint();
var waypoint1 = new_waypoint();
Line 402: Line 499:
or using a vector for each object, which has the added advantage that you can easily create arbitrary amounts of waypoints on demand:
or using a vector for each object, which has the added advantage that you can easily create arbitrary amounts of waypoints on demand:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
# this is our template for new waypoints
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
# this is our allocator function creating new waypoints
var new_waypoint = func {return {parents:[waypoint] };}
var new_waypoint = func {return {parents:[waypoint] };}


var waypoints = [0,0,0,0,0]; # initialize the vector to set its size
var waypoints = [nil,nil,nil,nil,nil]; # initialize the vector to set its size


var waypoints[0] = new_waypoint();
# populate the waypoints vector by allocating one new waypoint per "slot"
var waypoints[1] = new_waypoint();
waypoints[0] = new_waypoint();
var waypoints[2] = new_waypoint();
waypoints[1] = new_waypoint();
var waypoints[3] = new_waypoint();
waypoints[2] = new_waypoint();
var waypoints[4] = new_waypoint();
waypoints[3] = new_waypoint();
waypoints[4] = new_waypoint();
</syntaxhighlight>
</syntaxhighlight>
A shorter version would read:
<syntaxhighlight lang="nasal">
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var new_waypoint = func {return {parents:[waypoint] };}
var NUM_ELEMENTS = 5;
var waypoints = [];
setsize(waypoints, NUM_ELEMENTS);
forindex(var i; waypoints)
    waypoints[i] = new_waypoint();
</syntaxhighlight>


Once we start using a hash as a template for other hashes using the "parents" vector, we are actually creating a class that is copied to each new hash. This new copy of the class is called an "object" after creation.
Once we start using a hash as a template for other hashes using the "parents" vector, we are actually creating a class that is copied to each new hash. This new copy of the class is called an "object" after creation.
Line 419: Line 535:
A "class" is really just a template for a certain data type that consists of other data types and provides functions to work with the class. The functions publicly accessible are called its "interface" because these functions are meant to be used by the users of your class. This is in contrast to member fields which are usually not meant to be accessed directly.
A "class" is really just a template for a certain data type that consists of other data types and provides functions to work with the class. The functions publicly accessible are called its "interface" because these functions are meant to be used by the users of your class. This is in contrast to member fields which are usually not meant to be accessed directly.


Once a class is used as a template to create a new object, we say the class is getting "instantiated", i.e. an instance of the class (an actual object) is created. This makes it then possible to actually make use of its interface and access member functions (which are methods). Also see [http://flightgear.org/forums/viewtopic.php?f=30&t=14737#p145769].
Once a class is used as a template to create a new object, we say the class is getting "instantiated", i.e. an instance of the class (an actual object) is created. This makes it then possible to actually make use of its interface and access member functions (which are methods). Also see {{forum link|p=145769}}.




Now, given that the creation of new hashes using a template class is such a common thing to do - we could just as well add a new function to the parent hash that we can use to construct new hashes. As you could see already, the fields (or members) of a hash are specified in a well-defined form: "field_name:value".  
Now, given that the creation of new hashes using a template class is such a common thing to do - we could just as well add a new function to the parent hash that we can use to construct new hashes. As you could see already, the fields (or members) of a hash are specified in a well-defined form using the key/value format where key and value are separated by a colon: "field_name:value".  


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint1 = {  
var waypoint1 = {
number:1,
    number:1,
altitude:2,
    altitude:2,
distance:3,
    distance:3,
angle:4,
    angle:4,
length:5,
    length:5,
ID:6,
    ID:6,
bearing:7
    bearing:7
};
};
</syntaxhighlight>
</syntaxhighlight>
Line 438: Line 554:
This isn't any different for fields that are of type "function":
This isn't any different for fields that are of type "function":


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {  
var waypoint = {
number:1,
    number:1,
altitude:2,
    altitude:2,
distance:3,
    distance:3,
angle:4,
    angle:4,
length:5,
    length:5,
ID:6,
    ID:6,
bearing:7,
    bearing:7,
hello: func {
 
print("Hello");
    hello: func {
}
        print("Hello");
    }
};
};
</syntaxhighlight>
</syntaxhighlight>
Line 455: Line 572:
Note that we have added a new field named "hello" to our waypoint hash. This can be easily accessed and also called:
Note that we have added a new field named "hello" to our waypoint hash. This can be easily accessed and also called:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {  
var waypoint = {
number:1,
    number:1,
altitude:2,
    altitude:2,
distance:3,
    distance:3,
angle:4,
    angle:4,
length:5,
    length:5,
ID:6,
    ID:6,
bearing:7,
    bearing:7,
hello: func {
    hello: func {
print("Hello");
        print("Hello");
}
    }
};
};


var w = {parents: [waypoint]};
var w = {parents: [waypoint]};
w.hello();
w.hello();
</syntaxhighlight>
</syntaxhighlight>


On the other hand, we were just about to make construction of such hashes much simpler. This can be done by creating a new function that constructs hashes dynamically using a hash:
On the other hand, we were just about to make construction of such hashes much simpler. This can be done by creating a new function that constructs hashes dynamically using a hash:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
 
var make_waypoint = func {
var make_waypoint = func {
  return { parents:[waypoint] };
    return { parents:[waypoint] };
}
}
</syntaxhighlight>
</syntaxhighlight>


Next, we are going to change the function and and embed it into the hash, so that the constructor function becomes a part of the class, renaming it to "new":
Next, we are going to change the function and and embed it into the hash, so that the constructor function becomes a part of the class, renaming it to "new":


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {  
var waypoint = {
number:1,
    number:1,
altitude:2,
    altitude:2,
distance:3,
    distance:3,
angle:4,
    angle:4,
length:5,
    length:5,
ID:6,
    ID:6,
bearing:7,
    bearing:7,
new: func {
    new: func {
return {parents:[waypoint]}
        return {parents:[waypoint]}
}
    }
};
};
</syntaxhighlight>
</syntaxhighlight>
Line 504: Line 618:
So, whenever we need a new hash object of type "waypoint", we can simply call this construction function (which we'll call a constructor from now on):
So, whenever we need a new hash object of type "waypoint", we can simply call this construction function (which we'll call a constructor from now on):


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var wp = waypoint.new();
var wp = waypoint.new();
</syntaxhighlight>
</syntaxhighlight>
This is what most Nasal code using OOP will typically look like.
== Hashes and foreach (enumerating a hash) ==
To iterate through all elements in a hash the keys(hash) method is used to produce a vector of the keys for the hash.
<syntaxhighlight lang="nasal">
var namehash = {};
namehash["value1"]= 1;
namehash["value2"] =2;
foreach(var hash_key ; keys(namehash))
    print(namehash[hash_key]);
</syntaxhighlight>
== A generic constructor ==


Sometimes, you'll work with classes that do not have any custom constructor functions - but you can easily create a generic constructor, too:
Sometimes, you'll work with classes that do not have any custom constructor functions - but you can easily create a generic constructor, too:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var new = func(obj) {
    return {parents:obj};
}
</syntaxhighlight>
 
As long as '''obj''' is a vector of hashes, it can be simply invoked like this:
 
<syntaxhighlight lang="nasal">
var p = new( [Position3D] );
</syntaxhighlight>
 
Now, once you remember that Nasal implicitly has an '''arg''' vector for its arguments, the whole thing can be simplified like this:
 
<syntaxhighlight lang="nasal">
var new = func {
var new = func {
return {parents:arg};
    return {parents:arg};
}
 
var Position3D = {x:0.0, y:0.0, z:0.0};
 
var p = new( Position3D );
</syntaxhighlight>
 
This will simply create a function named "new" which accepts an implicit list of arguments (in the implicit "args" vector). The function returns a hash whose parents field is set to the args vector, so this method could even be used for multiple inheritance. Note that the Position3D hash no longer has its own constructor, it's really just a simple hash in this case.
 
To make this look a little nicer, we can also rename the default argument vector (i.e. args) to something more informative, like "classes":
 
<syntaxhighlight lang="nasal">
var new = func(classes...) {
    return {parents:classes};
}
}


Line 520: Line 679:
</syntaxhighlight>
</syntaxhighlight>


You could even implement an overloaded function that creates arrays of objects.
 
Using this approach, you could even make the "new operator" call custom constructors automatically for each created object.
 
You could even implement an overloaded function that creates arrays/vectors of objects.
 
A simple generic constructor can be expressed in a very succinct fashion:
 
<syntaxhighlight lang="nasal">
# a new function with an implicit arg parameter that simply returns a new hash with the parents field set to all arguments provided
var new = func return { parents:arg };
 
var Position3D = {x:0.0, y:0.0, z:0.0};
 
var p = new( Position3D );
</syntaxhighlight>


Now, let's imagine you want to add another member function to print a certain field's value, such as "number".  
Now, let's imagine you want to add another member function to print a certain field's value, such as "number".  
Given that objects created from the same class template, there needs to be a way for a function to refer to itself, i.e. to refer to the object itself, otherwise the function wouldn't know what object is meant.
Given that objects created from the same class template, there needs to be a way for a method (class function) to refer to itself (its own object), i.e. to refer to the object itself, otherwise the function wouldn't know what object is meant, and what object it is referring to.


This can be accomplished using the "me" keyword which ensures that the member function is always referring to the currently active object:
This can be accomplished using the "me" keyword which ensures that the member function is always referring to the currently active object (itself):


 
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="php">
var waypoint = {
var waypoint = {  
    number:1,
number:1,
    altitude:2,
altitude:2,
    distance:3,
distance:3,
    angle:4,
angle:4,
    length:5,
length:5,
    ID:6,
ID:6,
    bearing:7,
bearing:7,
    new: func {
new: func {
        return {parents:[waypoint]};
return {parents:[waypoint]};
    },
},
    show_number: func {
show_number: func {
        print(me.number);
print(me.number);
    }
}
};
};
</syntaxhighlight>
</syntaxhighlight>
Note how we are really just prepending the "me" keyword to the object's field, making it clear that "me" is the requested namespace/context of the field to be retrieved.


On the other hand, maybe you'd like to add some information (such as the number) during construction time to the object. So this would require changing the constructor function to accept a parameter, too:
On the other hand, maybe you'd like to add some information (such as the number) during construction time to the object. So this would require changing the constructor function to accept a parameter, too:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {  
var waypoint = {
number:1,
    number:1,
altitude:2,
    altitude:2,
distance:3,
    distance:3,
angle:4,
    angle:4,
length:5,
    length:5,
ID:6,
    ID:6,
bearing:7,
    bearing:7,
new: func(n) {
    new: func(n) {
return {parents:[waypoint]};
      return {parents:[waypoint]};
},
    },
show_number: func {
    show_number: func {
print(me.number);
        print(me.number);
}
    }
};
};
</syntaxhighlight>
</syntaxhighlight>
Line 570: Line 744:
Now, to actually set the number field during construction time to the value of n, you could create a temporary hash:
Now, to actually set the number field during construction time to the value of n, you could create a temporary hash:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
new: func(n) {
    new: func(n) {
var t={parents:[waypoint]};
        var t={parents:[waypoint]};
t.number = n;
        t.number = n;
return t;
        return t;
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 580: Line 754:
Or in its entirety:
Or in its entirety:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {  
var waypoint = {
number:1,
    number:1,
altitude:2,
    altitude:2,
distance:3,
    distance:3,
angle:4,
    angle:4,
length:5,
    length:5,
ID:6,
    ID:6,
bearing:7,
    bearing:7,
new: func(n) {
    new: func(n) {
var t={parents:[waypoint]};
        var t={parents:[waypoint]};
t.number = n;
        t.number = n;
return t;
        return t;
},
    },
show_number: func {
    show_number: func {
print(me.number);
        print(me.number);
}
    }
};
};
</syntaxhighlight>
</syntaxhighlight>
Line 603: Line 777:




<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {  
var waypoint = {
number:1,
    number:1,
altitude:2,
    altitude:2,
distance:3,
    distance:3,
angle:4,
    angle:4,
length:5,
    length:5,
ID:6,
    ID:6,
bearing:7,
    bearing:7,
new: func(n) {
    new: func(n) {
var t={parents:[waypoint]};
        var t={parents:[waypoint]};
t.number = n;
        t.number = n;
return t;
        return t;
},
    },
};
};


Line 624: Line 798:
Note how this creates a vector of 5 waypoints (0..4), each of these waypoints is a full object that can be conveniently accessed:
Note how this creates a vector of 5 waypoints (0..4), each of these waypoints is a full object that can be conveniently accessed:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
waypoints[0].bearing = 100;
waypoints[0].bearing = 100;
waypoints[1].altitude = 100;
waypoints[1].altitude = 100;
Line 632: Line 806:
You can also use Nasal's support for looping to conveniently process these waypoints:
You can also use Nasal's support for looping to conveniently process these waypoints:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {  
var waypoint = {
number:1,
    number:1,
altitude:2,
    altitude:2,
distance:3,
    distance:3,
angle:4,
    angle:4,
length:5,
    length:5,
ID:6,
    ID:6,
bearing:7,
    bearing:7,
new: func(n) {
    new: func(n) {
var t={parents:[waypoint]};
        var t={parents:[waypoint]};
t.number = n;
        t.number = n;
return t;
        return t;
},
    },
dump: func {
    dump: func {
print("Altitude:", me.altitude, " distance:", me.distance, " Bearing:", me.bearing);
        print("Altitude:", me.altitude, " distance:", me.distance, " Bearing:", me.bearing);
}
    }
};
};


Line 654: Line 828:


foreach(var w; waypoints) {
foreach(var w; waypoints) {
w.dump();
    w.dump();
}
}


Line 661: Line 835:
As you may remember, the parents keyword points not just to a single "class template" (hash), but instead to a vector of hashes. This makes it possible to use several different hashes as the template for a new hash:
As you may remember, the parents keyword points not just to a single "class template" (hash), but instead to a vector of hashes. This makes it possible to use several different hashes as the template for a new hash:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var new = func return {parents:arg};
var new = func return {parents:arg};


var propulsion = {horse_power:0};
var propulsion = {horse_power:0};
var vehicle = {tyres:0};
var vehicle = {tyres:0};


# create different propulsion classes:
# create different propulsion classes:
var engine = new(propulsion);
var engine = new(propulsion);
var piston_engine = new (engine);
var piston_engine = new (engine);
var turboprop_engine = new(engine);
var turboprop_engine = new(engine);
var jet_engine = new(engine);
var jet_engine = new(engine);


var car = new(vehicle, propulsion);
var car = new(vehicle, propulsion);
var boat = new(vehicle, propulsion);
var boat = new(vehicle, propulsion);
var airplane = new(vehicle, propulsion);
var airplane = new(vehicle, propulsion);


# seaplane is a new type of vehicle that inherits from the classes boat and airplane
# seaplane is a new type of vehicle that inherits from the classes boat and airplane
var seaplane = new(boat, airplane);
var seaplane = new(boat, airplane);
</syntaxhighlight>
</syntaxhighlight>


Line 694: Line 868:
The difference when dealing with these relationships is that whenever something "IS" something, the right thing to do is to directly create a new object using the parent class:
The difference when dealing with these relationships is that whenever something "IS" something, the right thing to do is to directly create a new object using the parent class:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var new = func return {parents:arg};
var new = func return {parents:arg};
var animal = {eyes:0,ears:0};
var animal = {eyes:0,ears:0};
 
var dog = new(animal);
var dog = new(animal);
var cat = new(animal);
var cat = new(animal);
var bird = new(animal);
var bird = new(animal);
var spider = new(animal);
var spider = new(animal);
</syntaxhighlight>
</syntaxhighlight>


Now, imagine we wanted to keep the number of legs for each animal, too: 4 legs for dogs and cats, 2 legs for birds and 8 legs for spiders:
Now, imagine we wanted to keep the number of legs for each animal, too: 4 legs for dogs and cats, 2 legs for birds and 8 legs for spiders:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var new = func return {parents:arg};
var new = func return {parents:arg};
var animal = {eyes:0,ears:0,legs:0};
var animal = {eyes:0,ears:0,legs:0};
 
var dog = new(animal);
var dog = new(animal);
var cat = new(animal);
var cat = new(animal);
var bird = new(animal);
var bird = new(animal);
var spider = new(animal);
var spider = new(animal);
</syntaxhighlight>
</syntaxhighlight>


However, at some point we may need additional information for each leg - such as the length of each leg. That's where we use a new "leg" class (an animal HAS legs), so that the animal class no longer just contains the number of legs, but rather contains a vector of "leg" objects:
However, at some point we may need additional information for each leg - such as the length of each leg. That's where we use a new "leg" class (an animal HAS legs), so that the animal class no longer just contains the number of legs, but rather contains a vector of "leg" objects:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var new = func return {parents:arg};
var new = func return {parents:arg};
var leg = {length:0};
var leg = {length:0};
var animal = {eyes:0,ears:0,legs:[] };
var animal = {eyes:0,ears:0,legs:[] };
 
var dog = new(animal);
var dog = new(animal);
var cat = new(animal);
var cat = new(animal);
var bird = new(animal);
var bird = new(animal);
var spider = new(animal);
var spider = new(animal);
</syntaxhighlight>
</syntaxhighlight>


This was a very simple introduction to object oriented programming in Nasal.
This was a very simple introduction to object oriented programming in Nasal.


 
[[Category:Nasal howto|Module, Create a new Nasal]]
[[Category:Howto|Nasal module, Create a new]]
[[Category:Nasal|Module, Create a new Nasal]]

Revision as of 20:35, 9 June 2019


Here we look into how to start using vectors and hashes in Nasal to simplify and generalize code. Vectors and hashes are two data structures that Nasal uses to store data. Vectors are in some other programming languages called for example lists or one-dimensional arrays, and hashes are similar to C's structs.

In many cases repetitive code can be generalized and rewritten into vectors, hashes or helper functions using them. Some of the advantages with rewriting repetitive code this way is that it helps maintenance by requiring less typing when modifying and/or when fixing bugs.

In addition to storing data hashes can "store" functions and be used as object classes. Object classes can be created by adding a function into a hash, that will create a new hash when called, a so called constructor (sometimes abbreviated to ctor or c'tor) usually named new().

Learn more about vectors

Imagine a piece of Nasal code working with 5 different waypoints. You would need to manage the data for each waypoint. A simple version using different variables for each data set might look like this:

var wp1 = 0;
var wp1alt = 0;
var wp1dist = 0;
var wp1angle = 0;
var wp1length = 0;
var wp1id = "";
var wp1brg = 0;

var wp2 = 0;
var wp2alt = 0;
var wp2dist = 0;
var wp2angle = 0;
var wp2length = 0;
var wp2id = "";
var wp2brg = 0;

var wp3 = 0;
var wp3alt = 0;
var wp3dist = 0;
var wp3angle = 0;
var wp3length = 0;
var wp3id = "";
var wp3brg = 0;

var wp4 = 0;
var wp4alt = 0;
var wp4dist = 0;
var wp4angle = 0;
var wp4length = 0;
var wp4id = "";
var wp4brg = 0;

var wp5 = 0;
var wp5alt = 0;
var wp5dist = 0;
var wp5angle = 0;
var wp5length = 0;
var wp5id = "";
var wp5brg = 0;

Now, this is fairly repetitive and not overly scalable, because the variable names are hard coded and need to be changed in a lot of places in the source code.

Just imagine we'd need to support more than just 5 waypoints, like maybe 10, 20, 50 or maybe even 1000 waypoints. As you can see, this method is very inflexible, complicated, tedious and error-prone.

Such code contains so called "code smells", another example would be code like this:

var l0 = "";
var l1 = "";
var l2 = "";
var l3 = "";
var l4 = "";
var l5 = "";
var l6 = "";
var l7 = "";
var l8 = "";
var l9 = "";
var l10 = "";
var l11 = "";
var l12 = "";
var l13 = "";
var l14 = "";
var l15 = "";
var l16 = "";
var l17 = "";
var l18 = "";
var l19 = "";
var l20 = "";

var hist1 = "";
var hist2 = "";
var hist3 = "";
var hist4 = "";
var hist5 = "";
var hist6 = "";

IDENTl3 = "";
IDENTl4 = "";
IDENTr4 = "";
IDENTl5 = "";
IDENTr5 = "";
IDENTl8 = "";

setprop("/instrumentation/efb/display/line0-l", l0);
setprop("/instrumentation/efb/display/line1-l", l1);
setprop("/instrumentation/efb/display/line2-l", l2);
setprop("/instrumentation/efb/display/line3-l", l3);
setprop("/instrumentation/efb/display/line4-l", l4);
setprop("/instrumentation/efb/display/line5-l", l5);
setprop("/instrumentation/efb/display/line6-l", l6);
setprop("/instrumentation/efb/display/line7-l", l7);
setprop("/instrumentation/efb/display/line8-l", l8);
setprop("/instrumentation/efb/display/line9-l", l9);
setprop("/instrumentation/efb/display/line10-l", l10);
setprop("/instrumentation/efb/display/line11-l", l11);
setprop("/instrumentation/efb/display/line12-l", l12);
setprop("/instrumentation/efb/display/line13-l", l13);
setprop("/instrumentation/efb/display/line14-l", l14);
setprop("/instrumentation/efb/display/line15-l", l15);
setprop("/instrumentation/efb/display/line16-l", l16);
setprop("/instrumentation/efb/display/line17-l", l17);
setprop("/instrumentation/efb/display/line18-l", l18);
setprop("/instrumentation/efb/display/line19-l", l19);
setprop("/instrumentation/efb/display/line20-l", l20);

Whenever you see such code, it should be obvious that using arrays/vectors would be a good idea.

So, it would be better to use a vector of waypoints instead. A vector is a list of things (variables) that can be easily accessed using an index into the vector. Pretty much like an array in C or C++, with the added advantage that the vector can be dynamically resized, e.g. using the setsize() library call. Consider the following example:


var waypoints = ["wp1", "wp2", "wp3", "wp4", "wp5"];

This piece of code is equivalent to creating 5 different variables named "waypoints[n]" with an access index from n=0 to 4 (5 elements in total).

As you can see below, indexing starts at 0.

The problem is, that this only gives us a list of single waypoints:

var waypoints = ["wp1", "wp2", "wp3", "wp4", "wp5"];
print(waypoints[0]); # print wp1
print(waypoints[1]); # print wp2
print(waypoints[2]); # print wp3
print(waypoints[3]); # print wp4
print(waypoints[4]); # print wp5

To do this using a conventional for loop, you would use the size() function to get the size of the vector:

var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
for(var index; index < size(waypoints); index=index+1) {
    print(waypoints[index]);
}

The same thing can be accomplished using the forindex loop:

var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
forindex(var index; waypoints) {
     print(waypoints[index]);
}

Now, waypoints being a vector, we can also use a simple foreach loop, that works without a counter, to process each element easily:

var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
foreach(var wpt; waypoints) {
     print(wpt);
}

What we really need to save all the other waypoint specific information is a new variable type that serves as the "container" for variables, so that we can save several variables for each waypoint.

var wp4 = 0;       # waypoint number
var wp4alt = 0;    # waypoint altitude
var wp4dist = 0;   # waypoint distance
var wp4angle = 0;  # waypoint angle
var wp4length = 0; # waypoint length
var wp4id = "";    # waypoint id
var wp4brg = 0;    # waypoint bearing

A vector based version

One simple way to accomplish this is using a another vector for each waypoint, nested inside the original vector. So that we end up with a two-dimensional data structure. For example, imagine a folder containing sub folders, with folders not having names but rather indices (numbers).

So we could be using a "main" vector to store all waypoints, while each field of the main vector is a different vector that contains all waypoint-specific data.

var wp4 = 0;       # waypoint number
var wp4alt = 0;    # waypoint altitude
var wp4dist = 0;   # waypoint distance
var wp4angle = 0;  # waypoint angle
var wp4length = 0; # waypoint length
var wp4id = "";    # waypoint id
var wp4brg = 0;    # waypoint bearing

var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg];
var waypoints = [waypoint4]

First of all, we are setting up all the different variables for wp4, next you are storing all variables in a vector called "waypoint4". In the end, we store this vector in another vector named "waypoints".

So, the very first vector element would be waypoints[0] and it would point to another vector (waypoint4), the elements of waypoint4 would be also available by index:

var wp4 = 1;         # waypoint number
var wp4alt = 1000;   # waypoint altitude
var wp4dist = 20.4;  # waypoint distance
var wp4angle = 33.4; #waypoint angle
var wp4length = 12;  # waypoint length
var wp4id = "none";  # waypoint id
var wp4brg = 122;    # waypoint bearing


var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg];
var waypoints = [waypoint4]

print ( waypoints[0][0] ) # contains the data stored in wp4
print ( waypoints[0][1] ) # contains the data stored in wp4alt
print ( waypoints[0][2] ) # contains the data stored in wp4dist
print ( waypoints[0][3] ) # contains the data stored in wp4angle
print ( waypoints[0][4] ) # contains the data stored in wp4length
print ( waypoints[0][5] ) # contains the data stored in wp4id
print ( waypoints[0][6] ) # contains the data stored in wp4brg

What we have here is a list of waypoints, with the first (0th) element containing another vector.

Obviously, you could add a bunch of other waypoints to the "waypoints" vector, too:

var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg];
var waypoint5 = [wp5,wp5alt,wp5dist,wp5angle,wp5length,wp5id, wp5brg];
var waypoints = [waypoint4, waypoint5]

The only problem with this approach is, that you'll need to set up all those wp variables - so there's a shorter version possible, by directly adding your data without using variables, i.e. "inline":

var waypoint4 = [1,1000,12,22,44,"none", 33];
var waypoint5 = [2,1500,22,42,14,"none", 133];
var waypoints = [waypoint4, waypoint5];

There's an even more succinct version possible. The next step would be to use embedded vectors directly:

var waypoints = [
 [1,1000,12,22,44,"none", 33], 
 [2,1500,22,42,14,"none", 133]
];

This is just formatted for clarity, the following snippet would be equivalent, but not as readable to people new to vectors:

var waypoints = [ [1,1000,12,22,44,"none", 33],  [2,1500,22,42,14,"none", 133] ];


Accessing such a vector with embedded (or nested) vectors is still as simple:

var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
print(waypoints[0][0]) # prints 1
print(waypoints[0][1]) # prints 1000
print(waypoints[1][0]) # prints 2
print(waypoints[1][1]) # prints 1500

The only issue here is that you'll need to keep vector ordering in mind, so that you can always access the right element number. This could be simplified by using some variables with telling names as the index for each vector:

# these are used self-explanatory indices - so that you don't need to remember each field's position:
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;

var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
print(waypoints[0][ALTITUDE]) # prints 1
print(waypoints[0][DISTANCE]) # prints 1000
print(waypoints[1][ALTITUDE]) # prints 2
print(waypoints[1][DISTANCE]) # prints 1500

Now, this yields already fairly readable source code:

var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]

# print the altitude for each waypoint in the vector:
foreach(var waypoint; waypoints) {
    print( waypoint[ALTITUDE] );
}

So, this would already be much better than our original version, because we can now have an arbitrary number of waypoints. New waypoints would need to be added to the waypoints vector by using the append() library function:

var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
print(waypoints[0][ALTITUDE]) # prints 1
print(waypoints[0][DISTANCE]) # prints 1000
print(waypoints[1][ALTITUDE]) # prints 2
print(waypoints[1][DISTANCE]) # prints 1500

append(waypoints, [3,3000,122,212,34,"none", 133] );

Next, we could come up with some helper functions to deal with the vector details transparently, using the same method as before: self-explanatory indices, which spares us having to remember the purpose of each field:

var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]];

var get = func (n,what) {
    return waypoints[n][what];
}

var set = func (n,what,value) {
    waypoints[n][what]=value;
}

print( get(0, ALTITUDE)  ); # prints 1
print( get(0, DISTANCE)  ); # prints 1000
print( get(1, ALTITUDE)  ); # prints 2
print( get(1, DISTANCE)  ); # prints 1500

There's one caveat here: The set function doesn't check the size of the vector, so it's possible that it tries to access elements that are not there. To fix this, you'd need to use a size(vec) call and check its size, and then adjust the size as needed using the setsize(vec,size) library function.


You could even add some more high level helpers for each waypoint field:

var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]];

var get = func (n,what) {
    return waypoints[n][what];
}

var set = func (n,what,value) {
    waypoints[n][what]=value;
}

var get_alt = func(n) {
    return get(n,ALTITUDE);
}

var get_dist = func(n) {
    return get(n,DISTANCE);
}

var get_angle = func(n) {
    return get(n,ANGLE);
}

var get_length = func(n) {
    return get(n,LENGTH);
}

var get_id = func(n) {
    return get(n,ID);
}

var get_brg = func(n) {
    return get(n,BEARING);
}


Obviously, there's still one issue though: we are using lots of variables and helpers that all belong to the "waypoints" type, but which clutter our source code.

So, next we are going to look into an even more flexible approach that nicely maps each waypoint field and all helper functions to a symbolic name, without having to remember vector indices and such: using Nasal hashes, which are easier to use than complex arrangements using nested vectors.

A hash based version (recommended)

Now, to wrap these fields into a single variable that serves as the container for other variables, we could use a Nasal hash and start completely from scratch.

Consider the following empty hash:

var waypoint = {};

Next, we are going to add some fields to our new variable "container". Each new field can be assigned an initial value, this is done by treating the field as a value/key pair, with a colon (:) separating both. And with multiple fields being separated by a comma (formatting only added for clarity):

# declare a new hash named waypoint
var waypoint = {
# add a few fields and assign a value to each field:
number:0,
altitude:0,
distance:0,
angle:0,
length:0,
ID:0,
bearing:0
};

which is equivalent to this, more succinct, version:

var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0};

This is already equivalent to our first example that used a vector to store this waypoint info, the only difference is here that we assign symbolic names (like number, altitude, distance etc) to each field, rather than just a numeric index, as was the case in the vector example.

This adds the following "member fields" to the waypoint hash and sets their initial value to 0:

  • number
  • altitude
  • distance
  • angle
  • length
  • ID
  • bearing

Now, to access any of these fields, we would use the "dot notation" by first specifying the name of the enclosing context (which is really just a fancy word for the name of the hash) and the name of the field we are interested in. You could read this as: LOCATION.FIELD (i.e. get FIELD out of location).

var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0};
print ( waypoint.number );
print ( waypoint.altitude );
print ( waypoint.distance );
print ( waypoint.angle );
print ( waypoint.length );
print ( waypoint.ID );
print ( waypoint.bearing );

So, the hash represents the surrounding environment (i.e. context or "namespace") in which these symbols are valid. For more information on namespaces, please see Namespaces and Methods.

Now, to make this is a little more interesting and to show what's happening, we are going to change the value of each field:

var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
print ( waypoint.number ); # prints 1
print ( waypoint.altitude ); # prints 2
print ( waypoint.distance ); # prints 3
print ( waypoint.angle ); # prints 4
print ( waypoint.length ); # 5
print ( waypoint.ID ); #6
print ( waypoint.bearing ); #7

So, you could obviously create several different versions of this hash to store your waypoint data:

var waypoint1 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint2 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint3 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint4 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint5 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};

This is already a pretty cool thing, because you would end up with 5 different containers all having their own set of fields, that you can access and set arbitrarily - without affecting the other containers.

But the really cool thing comes next:

As you may have noticed, the code (internal structure of fields) for each waypoint is 100% identical - so we could just as well tell the Nasal engine to use an existing hash as a TEMPLATE for a new object instead of having to replicate the hash over and over again. This is accomplished using the "parents" keyword:

var Position3D = {x:1.00, y:2.00, z:3.00};
var p1 = { parents: [Position3D] };
var p2 = { parents: [Position3D] };
var p3 = { parents: [Position3D] };

This creates three different new containers/hashes by copying the fields from the hash specified in the parents vector.

What parents will do is this: whenever a hash contains a vector field named "parents:" pointing to other hashes, it will look up the parent hashes and use them as a template and copy the fields of the parent hashes to the new hash, i.e. less typing for you!

This means, that the previously posted code could be easily abbreviated and written like this:

var waypoint1 = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var waypoint2 = {parents:[waypoint1] };
var waypoint3 = {parents:[waypoint1] };
var waypoint4 = {parents:[waypoint1] };
var waypoint5 = {parents:[waypoint1] };

You could even introduce a new helper function to create new objects, let's call it new_waypoint:

var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var new_waypoint = func() {
 return { parents:[waypoint] };
}

var waypoint1 = new_waypoint();
var waypoint2 = new_waypoint();
var waypoint3 = new_waypoint();
var waypoint4 = new_waypoint();
var waypoint5 = new_waypoint();


or using a vector for each object, which has the added advantage that you can easily create arbitrary amounts of waypoints on demand:

# this is our template for new waypoints
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};

# this is our allocator function creating new waypoints
var new_waypoint = func {return {parents:[waypoint] };}

var waypoints = [nil,nil,nil,nil,nil]; # initialize the vector to set its size

# populate the waypoints vector by allocating one new waypoint per "slot"
waypoints[0] = new_waypoint();
waypoints[1] = new_waypoint();
waypoints[2] = new_waypoint();
waypoints[3] = new_waypoint();
waypoints[4] = new_waypoint();

A shorter version would read:

var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7};
var new_waypoint = func {return {parents:[waypoint] };}

var NUM_ELEMENTS = 5;
var waypoints = [];
setsize(waypoints, NUM_ELEMENTS);

forindex(var i; waypoints)
    waypoints[i] = new_waypoint();


Once we start using a hash as a template for other hashes using the "parents" vector, we are actually creating a class that is copied to each new hash. This new copy of the class is called an "object" after creation.

A "class" is really just a template for a certain data type that consists of other data types and provides functions to work with the class. The functions publicly accessible are called its "interface" because these functions are meant to be used by the users of your class. This is in contrast to member fields which are usually not meant to be accessed directly.

Once a class is used as a template to create a new object, we say the class is getting "instantiated", i.e. an instance of the class (an actual object) is created. This makes it then possible to actually make use of its interface and access member functions (which are methods). Also see [1] This is a link to the FlightGear forum..


Now, given that the creation of new hashes using a template class is such a common thing to do - we could just as well add a new function to the parent hash that we can use to construct new hashes. As you could see already, the fields (or members) of a hash are specified in a well-defined form using the key/value format where key and value are separated by a colon: "field_name:value".

var waypoint1 = {
    number:1,
    altitude:2,
    distance:3,
    angle:4,
    length:5,
    ID:6,
    bearing:7
};

This isn't any different for fields that are of type "function":

var waypoint = {
    number:1,
    altitude:2,
    distance:3,
    angle:4,
    length:5,
    ID:6,
    bearing:7,

    hello: func {
        print("Hello");
    }
};

Note that we have added a new field named "hello" to our waypoint hash. This can be easily accessed and also called:

var waypoint = {
    number:1,
    altitude:2,
    distance:3,
    angle:4,
    length:5,
    ID:6,
    bearing:7,
    hello: func {
        print("Hello");
    }
};

var w = {parents: [waypoint]};
w.hello();

On the other hand, we were just about to make construction of such hashes much simpler. This can be done by creating a new function that constructs hashes dynamically using a hash:

var make_waypoint = func {
    return { parents:[waypoint] };
}

Next, we are going to change the function and and embed it into the hash, so that the constructor function becomes a part of the class, renaming it to "new":

var waypoint = {
    number:1,
    altitude:2,
    distance:3,
    angle:4,
    length:5,
    ID:6,
    bearing:7,
    new: func {
        return {parents:[waypoint]}
    }
};


So, whenever we need a new hash object of type "waypoint", we can simply call this construction function (which we'll call a constructor from now on):

var wp = waypoint.new();

This is what most Nasal code using OOP will typically look like.

Hashes and foreach (enumerating a hash)

To iterate through all elements in a hash the keys(hash) method is used to produce a vector of the keys for the hash.

var namehash = {};
namehash["value1"]= 1;
namehash["value2"] =2;

foreach(var hash_key ; keys(namehash))
    print(namehash[hash_key]);

A generic constructor

Sometimes, you'll work with classes that do not have any custom constructor functions - but you can easily create a generic constructor, too:

var new = func(obj) {
    return {parents:obj};
}

As long as obj is a vector of hashes, it can be simply invoked like this:

var p = new( [Position3D] );

Now, once you remember that Nasal implicitly has an arg vector for its arguments, the whole thing can be simplified like this:

var new = func {
    return {parents:arg};
}

var Position3D = {x:0.0, y:0.0, z:0.0};

var p = new( Position3D );

This will simply create a function named "new" which accepts an implicit list of arguments (in the implicit "args" vector). The function returns a hash whose parents field is set to the args vector, so this method could even be used for multiple inheritance. Note that the Position3D hash no longer has its own constructor, it's really just a simple hash in this case.

To make this look a little nicer, we can also rename the default argument vector (i.e. args) to something more informative, like "classes":

var new = func(classes...) {
    return {parents:classes};
}

var Position3D = {x:0.0, y:0.0, z:0.0};

var p = new( Position3D );


Using this approach, you could even make the "new operator" call custom constructors automatically for each created object.

You could even implement an overloaded function that creates arrays/vectors of objects.

A simple generic constructor can be expressed in a very succinct fashion:

# a new function with an implicit arg parameter that simply returns a new hash with the parents field set to all arguments provided
var new = func return { parents:arg };

var Position3D = {x:0.0, y:0.0, z:0.0};

var p = new( Position3D );

Now, let's imagine you want to add another member function to print a certain field's value, such as "number". Given that objects created from the same class template, there needs to be a way for a method (class function) to refer to itself (its own object), i.e. to refer to the object itself, otherwise the function wouldn't know what object is meant, and what object it is referring to.

This can be accomplished using the "me" keyword which ensures that the member function is always referring to the currently active object (itself):

var waypoint = {
    number:1,
    altitude:2,
    distance:3,
    angle:4,
    length:5,
    ID:6,
    bearing:7,
    new: func {
        return {parents:[waypoint]};
    },
    show_number: func {
        print(me.number);
    }
};

Note how we are really just prepending the "me" keyword to the object's field, making it clear that "me" is the requested namespace/context of the field to be retrieved.

On the other hand, maybe you'd like to add some information (such as the number) during construction time to the object. So this would require changing the constructor function to accept a parameter, too:

var waypoint = {
    number:1,
    altitude:2,
    distance:3,
    angle:4,
    length:5,
    ID:6,
    bearing:7,
    new: func(n) {
       return {parents:[waypoint]};
    },
    show_number: func {
        print(me.number);
    }
};

Note how the new function has been changed to accept a parameter named "n".

Now, to actually set the number field during construction time to the value of n, you could create a temporary hash:

    new: func(n) {
        var t={parents:[waypoint]};
        t.number = n;
        return t;
}

Or in its entirety:

var waypoint = {
    number:1,
    altitude:2,
    distance:3,
    angle:4,
    length:5,
    ID:6,
    bearing:7,
    new: func(n) {
        var t={parents:[waypoint]};
        t.number = n;
        return t;
    },
    show_number: func {
        print(me.number);
    }
};

Now, back to your initial example regarding a list of waypoints:


var waypoint = {
    number:1,
    altitude:2,
    distance:3,
    angle:4,
    length:5,
    ID:6,
    bearing:7,
    new: func(n) {
        var t={parents:[waypoint]};
        t.number = n;
        return t;
    },
};

var waypoints = [waypoint.new(), waypoint.new(), waypoint.new(), waypoint.new(), waypoint.new() ];

Note how this creates a vector of 5 waypoints (0..4), each of these waypoints is a full object that can be conveniently accessed:

waypoints[0].bearing = 100;
waypoints[1].altitude = 100;
waypoints[2].ID = "none";

You can also use Nasal's support for looping to conveniently process these waypoints:

var waypoint = {
    number:1,
    altitude:2,
    distance:3,
    angle:4,
    length:5,
    ID:6,
    bearing:7,
    new: func(n) {
        var t={parents:[waypoint]};
        t.number = n;
        return t;
    },
    dump: func {
        print("Altitude:", me.altitude, " distance:", me.distance, " Bearing:", me.bearing);
    }
};

var waypoints = [waypoint.new(), waypoint.new(), waypoint.new(), waypoint.new(), waypoint.new(), ];

foreach(var w; waypoints) {
    w.dump();
}

As you may remember, the parents keyword points not just to a single "class template" (hash), but instead to a vector of hashes. This makes it possible to use several different hashes as the template for a new hash:

var new = func return {parents:arg};

var propulsion = {horse_power:0};
var vehicle = {tyres:0};

# create different propulsion classes:
var engine = new(propulsion);
var piston_engine = new (engine);
var turboprop_engine = new(engine);
var jet_engine = new(engine);

var car = new(vehicle, propulsion);
var boat = new(vehicle, propulsion);
var airplane = new(vehicle, propulsion);

# seaplane is a new type of vehicle that inherits from the classes boat and airplane
var seaplane = new(boat, airplane);


There are two important concepts to keep in mind here: The relationship between classes is classified according to an "IS A" and "HAS A" pattern:

  • a car "IS A" vehicle
  • an airplane "IS A" vehicle
  • an elephant "IS AN" animal
  • a car "HAS AN" engine (0..x)
  • an airplane "HAS AN" engine (0..x)
  • an elephant "HAS" ears

The difference when dealing with these relationships is that whenever something "IS" something, the right thing to do is to directly create a new object using the parent class:

var new = func return {parents:arg};
var animal = {eyes:0,ears:0};

var dog = new(animal);
var cat = new(animal);
var bird = new(animal);
var spider = new(animal);

Now, imagine we wanted to keep the number of legs for each animal, too: 4 legs for dogs and cats, 2 legs for birds and 8 legs for spiders:

var new = func return {parents:arg};
var animal = {eyes:0,ears:0,legs:0};

var dog = new(animal);
var cat = new(animal);
var bird = new(animal);
var spider = new(animal);

However, at some point we may need additional information for each leg - such as the length of each leg. That's where we use a new "leg" class (an animal HAS legs), so that the animal class no longer just contains the number of legs, but rather contains a vector of "leg" objects:

var new = func return {parents:arg};
var leg = {length:0};
var animal = {eyes:0,ears:0,legs:[] };

var dog = new(animal);
var cat = new(animal);
var bird = new(animal);
var spider = new(animal);

This was a very simple introduction to object oriented programming in Nasal.