20,741
edits
mNo edit summary |
mNo edit summary |
||
| Line 5: | Line 5: | ||
Imagine, three houses with rooms. | Imagine, three houses with rooms. | ||
Even otherwise identical rooms are | Even otherwise identical rooms are kept in separate "environments", i.e. you have to refer to a certain "environment" (namespace) to uniquely identify a certain room. | ||
For example, imagine three bath rooms in these three houses: house1, house2, house3. | For example, imagine three bath rooms in these three houses: house1, house2, house3. | ||
Now, imagine the users of the houses | 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. | ||
| Line 18: | Line 18: | ||
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. | ||
Basically, namespaces make it possible to have identically named variables/symbols without them possibly "clashing" or "polluting" the global namespace. | Basically, namespaces make it possible to have identically named variables/symbols without them possibly "clashing" or "polluting" the global namespace. Imagine a namespace to be a container with its own variables and scope. So certain variables are only valid within that particular namespace. | ||
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: | ||
| Line 28: | Line 28: | ||
These naming conventions can become awkward pretty quickly. | These naming conventions can become awkward pretty quickly. | ||
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) | 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). | ||
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 | In Nasal space, variables declared in functions will by default be specific to the function's scope and not be directly accessible: | ||
| Line 39: | Line 39: | ||
} | } | ||
Here, x is declared to be specific to the "hello" function and its namespace. | 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. | ||
| Line 61: | Line 61: | ||
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). | |||
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). | ||