Howto:Understand Namespaces and Methods: Difference between revisions

Jump to navigation Jump to search
Use Nasal syntax-highlighting
No edit summary
(Use Nasal syntax-highlighting)
Line 9: Line 9:


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.
 
<syntaxhighlight lang="nasal">
  house1.bath
house1.bath
  house2.bath
house2.bath
  house3.bath
house3.bath
 
</syntaxhighlight>
Here, houseX is the enclosing namespace/context, i.e. "bath" is a member or "field" of the namespace.
Here, houseX is the enclosing namespace/context, i.e. "bath" is a member or "field" of the namespace.


Line 19: Line 19:


Otherwise, you would have to use different symbols with explicit naming conventions, such as:
Otherwise, you would have to use different symbols with explicit naming conventions, such as:
 
<syntaxhighlight lang="nasal">
house1_bath
house1_bath
house2_bath
house2_bath
house3_bath
house3_bath
</syntaxhighlight>


As you can see, these naming conventions can become awkward and inflexible pretty quickly:
As you can see, these naming conventions can become awkward and inflexible pretty quickly:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
       var wp1 = 0;
       var wp1 = 0;
       var wp1alt = 0;
       var wp1alt = 0;
Line 73: Line 74:
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="nasal">
     var hello = func {
     var hello = func {
       var x =100; # x is local variable specific to the scope of the currently executing function
       var x =100; # x is local variable specific to the scope of the currently executing function
Line 81: Line 82:
Here, x is declared to be specific to the "hello" function and its namespace, 'x' will only be valid and visible during the lifetime of function, i.e. at execution time. Consequently, this means that accesses to such variables will be invalid:
Here, x is declared to be specific to the "hello" function and its namespace, 'x' will only be valid and visible during the lifetime of function, i.e. at execution time. Consequently, this means that accesses to such variables will be invalid:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
     var hello = func {
     var hello = func {
       var x =100; # x is local variable specific to the scope of the currently executing function
       var x =100; # x is local variable specific to the scope of the currently executing function
Line 91: Line 92:
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:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  var foo = {};
  var foo = {};
</syntaxhighlight>
</syntaxhighlight>
Line 97: Line 98:
To add members or fields to this "context" (or namespace), you can use several different notations, such as using conventional assignment:
To add members or fields to this "context" (or namespace), you can use several different notations, such as using conventional assignment:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  foo.altitude = 100;  
  foo.altitude = 100;  
  foo["altitude"] = 100;
  foo["altitude"] = 100;
Line 104: Line 105:
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):


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  var foo = { altitude:100 };
  var foo = { altitude:100 };
</syntaxhighlight>
</syntaxhighlight>
Line 110: Line 111:
To add multiple keys to such a dictionary, you just separate them using a comma:
To add multiple keys to such a dictionary, you just separate them using a comma:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  var foo = { altitude:100, latitude:0, longitude:0 };
  var foo = { altitude:100, latitude:0, longitude:0 };
</syntaxhighlight>
</syntaxhighlight>
Line 116: Line 117:
To make this easier you can always add a trailing comma, which is still valid Nasal:
To make this easier you can always add a trailing comma, which is still valid Nasal:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  var foo = { altitude:100, latitude:0, longitude:0, };
  var foo = { altitude:100, latitude:0, longitude:0, };
</syntaxhighlight>
</syntaxhighlight>
Line 122: Line 123:
In addition, it is valid to omit the key's value too:
In addition, it is valid to omit the key's value too:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  var foo = { altitude:, latitude:, longitude:, };
  var foo = { altitude:, latitude:, longitude:, };
</syntaxhighlight>
</syntaxhighlight>
Line 128: Line 129:
Which will be equivalent to initializing the value to '''nil''':
Which will be equivalent to initializing the value to '''nil''':


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">
  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:
 
<syntaxhighlight lang="nasal">
print ( foo.altitude );
print ( foo.altitude );
 
</syntaxhighlight>
So, basically namespaces are all about organizing and structuring your variables and the overall symbol space.
So, basically namespaces are all about organizing and structuring your variables and the overall symbol space.
In object oriented programming, this concept is very powerful because you cannot only have hash-specific variables but also hash-specific functions. This makes it possible to create new objects by using a template hash and inheriting fields and behavior (methods).
In object oriented programming, this concept is very powerful because you cannot only have hash-specific variables but also hash-specific functions. This makes it possible to create new objects by using a template hash and inheriting fields and behavior (methods).
Line 147: Line 148:


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:
 
<syntaxhighlight lang="nasal">
  house.bath.switch_off_lights
house.bath.switch_off_lights
</syntaxhighlight>


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.

Navigation menu