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

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
mNo edit summary
Line 552: Line 552:
</syntaxhighlight>
</syntaxhighlight>


Note how this create 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="php">
Line 589: Line 589:
</syntaxhighlight>
</syntaxhighlight>


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">
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);
</syntaxhighlight>
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:
<syntaxhighlight lang="php">
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);
</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:
<syntaxhighlight lang="php">
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);
</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:
<syntaxhighlight lang="php">
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);
</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.

Navigation menu