Howto:Understand Namespaces and Methods: Difference between revisions

Jump to navigation Jump to search
Category, general cleanup
(Category, general cleanup)
Line 1: Line 1:
== Namespaces ==
== Namespaces ==
A namespace is a "context", i.e. an environment where a certain symbol (variable) is valid.
A namespace is a "context", i.e. an environment where a certain symbol (variable) is valid.


Line 10: Line 8:


Now, imagine the users of the houses wanting to refer to each bath room using just the word (symbol/variable) "bath". To be able to resolve this properly, they will also need to qualify what house they are referring to, i.e. if it's house1, house2, house3.
Now, imagine the users of the houses wanting to refer to each bath room using just the word (symbol/variable) "bath". To be able to resolve this properly, they will also need to qualify what house they are referring to, i.e. if it's house1, house2, house3.


   house1.bath
   house1.bath
Line 69: Line 66:
       var wp5brg = 0;
       var wp5brg = 0;
</syntaxhighlight>
</syntaxhighlight>


Before namespaces were used, there were only such "global variables", so whenever some piece of code referred to a variable, it was possible to clash with some other similar or even unrelated uses of the variable (imagine a counter variable) somewhere else in the source code.
Before namespaces were used, there were only such "global variables", so whenever some piece of code referred to a variable, it was possible to clash with some other similar or even unrelated uses of the variable (imagine a counter variable) somewhere else in the source code.
Line 75: Line 71:
That's when people started providing a surrounding "context" to embed variables properly.
That's when people started providing a surrounding "context" to embed variables properly.
In Nasal space, variables declared in functions will by default be specific to the function's scope and not be directly accessible:
In Nasal space, variables declared in functions will by default be specific to the function's scope and not be directly accessible:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Line 92: Line 87:
     print(x);
     print(x);
</syntaxhighlight>
</syntaxhighlight>


In Nasal, a namespace is just a conventional hash, which is a fancy word for a dictionary. A dictionary stores values in the form of key/value pairs, each value is linked to a key that can be used to look up said value:
In Nasal, a namespace is just a conventional hash, which is a fancy word for a dictionary. A dictionary stores values in the form of key/value pairs, each value is linked to a key that can be used to look up said value:
Line 105: Line 99:
  foo.altitude = 100;  
  foo.altitude = 100;  
  foo["altitude"] = 100;
  foo["altitude"] = 100;
<syntaxhighlight lang="php">
</syntaxhighlight>


or even specify fields during initialization using a key, colon, value notation (key:value):
or even specify fields during initialization using a key, colon, value notation (key:value):
Line 136: Line 130:
  var foo = { altitude:nil, latitude:nil, longitude:nil, };
  var foo = { altitude:nil, latitude:nil, longitude:nil, };
</syntaxhighlight>
</syntaxhighlight>


In order to access these fields or "members" of a namespace, you need to provide a valid namespace first:
In order to access these fields or "members" of a namespace, you need to provide a valid namespace first:
Line 148: Line 140:
Just imagine it like an "area code", where people living in different countries, states, districts, counties, towns get a DIFFERENT prefix code, even though they may have the same telephone number otherwise. The international prefix number (i.e. 001 for the US) makes it obvious that the following number is a US number, same goes for local area codes (752, 642, 543).
Just imagine it like an "area code", where people living in different countries, states, districts, counties, towns get a DIFFERENT prefix code, even though they may have the same telephone number otherwise. The international prefix number (i.e. 001 for the US) makes it obvious that the following number is a US number, same goes for local area codes (752, 642, 543).


 
See [[Nasal scripting language#Namespaces]] for more information.
'''Click ''[http://wiki.flightgear.org/Nasal_scripting_language#Namespaces here]'' for more information.'''


== Methods ==
== Methods ==
Methods are somewhat related to "namespaces" in that they are class-specific functions (OOP), i.e. functions that are specific to a certain instance of an already instantiated class. In Nasal space, this means that the function is embedded inside a Nasal hash and that it makes use of instance data (using "me") or accessing the parents vector.
Methods are somewhat related to "namespaces" in that they are class-specific functions (OOP), i.e. functions that are specific to a certain instance of an already instantiated class. In Nasal space, this means that the function is embedded inside a Nasal hash and that it makes use of instance data (using "me") or accessing the parents vector.


For example, to switch off the lights in the bath room, there could be a method "switch_off_lights" in the "house" class:
For example, to switch off the lights in the bath room, there could be a method "switch_off_lights" in the "house" class:


   house.bath.switch_off_lights
   house.bath.switch_off_lights


Obviously, this will only work if the switch off routine (method!) has some house to work with. The class itself really is just a "template" for functionality, before it can be used it needs to be instantiated, i.e. a new object (house) must be created using the template, and then the member functions (methods) can be called for that object.
Obviously, this will only work if the switch off routine (method!) has some house to work with. The class itself really is just a "template" for functionality, before it can be used it needs to be instantiated, i.e. a new object (house) must be created using the template, and then the member functions (methods) can be called for that object.


 
See [[Nasal scripting language#More on methods]] for more information.
'''Click ''[http://wiki.flightgear.org/Nasal_scripting_language#More_on_methods here]'' for more information.'''


An tutorial along with source code examples going into more detail is available here: [[Howto: Start using vectors and hashes in_Nasal]].
An tutorial along with source code examples going into more detail is available here: [[Howto: Start using vectors and hashes in_Nasal]].


== More Information ==
== External links ==
 
* [http://en.wikipedia.org/wiki/Namespace Namespace] (Wikipedia)
 
* [http://en.wikipedia.org/wiki/Method_%28computer_programming%29 Method] (Wikipedia)
http://en.wikipedia.org/wiki/Namespace
* [http://en.wikipedia.org/wiki/Object-oriented_programming Object-oriented programming] (Wikipedia)
 
http://en.wikipedia.org/wiki/Method_%28computer_programming%29
 
http://en.wikipedia.org/wiki/Object-oriented_programming


== Resources ==
== Resources ==
This article would not have been possible without FlightGear forum member Hooray and [http://www.flightgear.org/forums/viewtopic.php?f=30&t=14737 this topic].


This article would not have been possible without FG Forum Member Hooray:
[[Category:Nasal howto]]
 
http://www.flightgear.org/forums/viewtopic.php?f=30&t=14737
 
 
[[Category:Nasal]]
[[Category:Howto]]

Navigation menu