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

Jump to navigation Jump to search
Use Nasal syntax-highlighting
(Use Nasal syntax-highlighting)
Line 6: Line 6:
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;
Line 53: Line 53:


Such code contains so called "code smells", another example would be code like this:
Such code contains so called "code smells", another example would be code like this:
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var l0 = "";
var l0 = "";
var l1 = "";
var l1 = "";
Line 118: Line 118:




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


Line 128: Line 128:
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) {
Line 148: Line 148:
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) {
Line 157: Line 157:
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) {
Line 166: Line 166:
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
Line 182: Line 182:
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
Line 199: Line 199:
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
Line 225: Line 225:
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 233: Line 233:
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];
Line 241: Line 241:
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 = [
var waypoints = [
  [1,1000,12,22,44,"none", 33],  
  [1,1000,12,22,44,"none", 33],  
Line 250: Line 250:
This is just formatted for clarity, the following snippet would be equivalent, but not as readable to people new to vectors:
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] ];
</syntaxhighlight>
</syntaxhighlight>
Line 257: Line 257:
Accessing such a vector with embedded (or nested) vectors is still as simple:
Accessing such a vector with embedded (or nested) vectors is still as simple:


<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
print(waypoints[0][0]) # prints 1
Line 267: Line 267:
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">
# these are used self-explanatory indices - so that you don't need to remember each field's position:
# 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 NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
Line 280: Line 280:
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]]
Line 292: Line 292:
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]]
Line 305: Line 305:
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:
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]];
Line 328: Line 328:
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]];
Line 376: Line 376:
Consider the following empty hash:
Consider the following empty hash:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {};
var waypoint = {};
</syntaxhighlight>
</syntaxhighlight>
Line 382: Line 382:
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):
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="php">
<syntaxhighlight lang="nasal">
# declare a new hash named waypoint
# declare a new hash named waypoint
var waypoint = {
var waypoint = {
Line 397: Line 397:


which is equivalent to this, more succinct, version:
which is equivalent to this, more succinct, version:
<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};
</syntaxhighlight>
</syntaxhighlight>
Line 414: Line 414:
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 429: Line 429:
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 442: Line 442:
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 456: Line 456:
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:
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 469: Line 469:
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 479: Line 479:
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() {
var new_waypoint = func() {
Line 495: Line 495:
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
# 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};
Line 514: Line 514:
A shorter version would read:
A shorter version would read:


<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] };}
Line 536: Line 536:
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".  
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,
Line 550: Line 550:
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,
Line 568: Line 568:
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,
Line 588: Line 588:
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] };
Line 596: Line 596:
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,
Line 614: Line 614:
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>
Line 624: Line 624:
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) {
var new = func(obj) {
     return {parents:obj};
     return {parents:obj};
Line 632: Line 632:
As long as '''obj''' is a vector of hashes, it can be simply invoked like this:
As long as '''obj''' is a vector of hashes, it can be simply invoked like this:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var p = new( [Position3D] );
var p = new( [Position3D] );
</syntaxhighlight>
</syntaxhighlight>
Line 638: Line 638:
Now, once you remember that Nasal implicitly has an '''arg''' vector for its arguments, the whole thing can be simplified like this:
Now, once you remember that Nasal implicitly has an '''arg''' vector for its arguments, the whole thing can be simplified like this:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var new = func {
var new = func {
     return {parents:arg};
     return {parents:arg};
Line 652: Line 652:
To make this look a little nicer, we can also rename the default argument vector (i.e. args) to something more informative, like "classes":
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="php">
<syntaxhighlight lang="nasal">
var new = func(classes...) {
var new = func(classes...) {
     return {parents:classes};
     return {parents:classes};
Line 669: Line 669:
A simple generic constructor can be expressed in a very succinct fashion:
A simple generic constructor can be expressed in a very succinct fashion:


<syntaxhighlight lang="php">
<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
# 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 new = func return { parents:arg };
Line 683: Line 683:
This can be accomplished using the "me" keyword which ensures that the member function is always referring to the currently active object (itself):
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="php">
<syntaxhighlight lang="nasal">
var waypoint = {
var waypoint = {
     number:1,
     number:1,
Line 705: Line 705:
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,
Line 727: Line 727:
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]};
Line 737: Line 737:
Or in its entirety:
Or in its entirety:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {
var waypoint = {
     number:1,
     number:1,
Line 760: Line 760:




<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
var waypoint = {
var waypoint = {
     number:1,
     number:1,
Line 781: Line 781:
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 789: Line 789:
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,
Line 818: Line 818:
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};


Line 851: Line 851:
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};
Line 863: Line 863:
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};
Line 875: Line 875:
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};

Navigation menu