Board index FlightGear Development Nasal

What are name spaces and methods?

Nasal is the scripting language of FlightGear.

What are name spaces and methods?

Postby Rupert » Tue Dec 20, 2011 10:25 pm

Tried looking up name spaces in the Wiki, nothing. Same for name spaces.

Specifically Global and local for name spaces. I don't know whether there are global and local methods?

Can someone please explain simply what these are, what they are used for and why they are needed?

Please do not make the explanation too complicated but not too dum either.

Sorry for sounding like a complete n00b.

Cheers,
Rupert
Rupert
 
Posts: 39
Joined: Fri Dec 02, 2011 4:21 am

Re: What are name spaces and methods?

Postby Hooray » Wed Dec 21, 2011 9:59 am

A name space is a "context", i.e. an environment where a certain symbol is valid.

Imagine, three houses with rooms.
Even otherwise identical rooms are kepts 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.

Now, imagine the users of the houses want 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
house2.bath
house3.bath

Basically, namespaces make it possible to have identically named variables/symbols without them possibly "clashing" or "polluting" the global namespace.

Before namespaces were used, there were only "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.
In Nasal space, variables declared in functions will by default by specific to the function's scope and not be directly accessible:

Code: Select all

var hello = func {
  var x =100;
}



Here, x is declared to be specific to the "hello" function and its namespace.

So, basically namespaces are all about organizing and structuring your variables and the overall symbol space.

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).

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:

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 must be created using the template, and then the member functions (methods) can be called.


http://en.wikipedia.org/wiki/Namespace
http://en.wikipedia.org/wiki/Method_%28 ... ramming%29
http://en.wikipedia.org/wiki/Object-ori ... rogramming
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11329
Joined: Tue Mar 25, 2008 8:40 am

Re: What are name spaces and methods?

Postby Rupert » Wed Dec 21, 2011 11:02 am

Hooray!!! I finally know what they are now!

Hooray, you are awesome.

Thanks for especially providing the domestic examples.

Cheers,
Rupy
Rupert
 
Posts: 39
Joined: Fri Dec 02, 2011 4:21 am

Re: What are name spaces and methods?

Postby Hooray » Wed Dec 21, 2011 1:11 pm

There's another introductory article on OOP that I wrote a while ago for the FG newsletter to provide a layman's introduction to OOP in Nasal: http://wiki.flightgear.org/Object_orien ... g_in_Nasal

Some of what I wrote here should probably added to that article, feel free to review/improve the article (you only need to register a wiki account) or let me know if there's anything else unclear.

I am fine answering any related questions, especially if you are fine turning my responses into useful wiki docs, i.e. by copying/pasting my comments to said article.

Obviously, these things are not very hard to understand, but the problem is that people who already understand them, fail to use proper (=easy) terminology, so your insight is definitely appreciated.

If you are in any way interested in Nasal programming, there's lots to be read and to be understood available in the wiki.

Recently, some of us spent some time to improve on the Nasal docs, so if you find anything unclear/missing, please get in touch with us so that we can hopefully fix things.

Obviously, just going ahead and editing existing articles is just as much appreciated!
Last edited by Hooray on Thu Dec 22, 2011 8:42 am, edited 1 time in total.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11329
Joined: Tue Mar 25, 2008 8:40 am

Re: What are name spaces and methods?

Postby Rupert » Thu Dec 22, 2011 12:18 am

Is it ok if I make a new article altogether and call it name spaces and methods and copy this useful information in?

Especially the examples because if you hadn't put those in, I would have had no idea what you were talking on about.

Just need permission from you Hooray.

Cheers,
Rupy
Rupert
 
Posts: 39
Joined: Fri Dec 02, 2011 4:21 am

Re: What are name spaces and methods?

Postby Hooray » Thu Dec 22, 2011 8:39 am

Anything I post here publicly is FG related, and so you are also free to reuse it to improve the documentation, it's in fact appreciated. And I think this is something that all users here sort of agree to by participating in discussions. In fact, I have done this myself numerous times already, often without asking at all - because that'll taken even more time than just a simple copy/paste.

Thanks
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11329
Joined: Tue Mar 25, 2008 8:40 am

Re: What are name spaces and methods?

Postby Rupert » Fri Dec 23, 2011 1:46 am

Rupert
 
Posts: 39
Joined: Fri Dec 02, 2011 4:21 am

Re: What are name spaces and methods?

Postby Hooray » Fri Dec 23, 2011 11:29 am

Thanks, I have added some more comments.

Also, it seems you are pretty familiar with wiki editing - your article looks much better than what I usually publish ;-)
So if you have any other Nasal related questions, please let me know - I don't mind answering them in excruciating detail as long you improve our docs then :-)

Seriously, people who are able to turn conversations into articles are obviously highly appreciated in an open source project, simply because they can help creating and updating the documentation, which often is a fairly lacking part of OSS.

So, if you find the time to review our Nasal articles (because Nasal coding seems to be what you are interested in), please let us know if there's anything that we should improve eventually.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11329
Joined: Tue Mar 25, 2008 8:40 am

Re: What are name spaces and methods?

Postby Rupert » Fri Dec 23, 2011 12:22 pm

Hooray wrote in Fri Dec 23, 2011 11:29 am:Thanks, I have added some more comments.



Your welcome. ;) And feel free to.

Hooray wrote in Fri Dec 23, 2011 11:29 am:
Also, it seems you are pretty familiar with wiki editing - your article looks much better than what I usually publish ;-)


Thanks.

Hooray wrote in Fri Dec 23, 2011 11:29 am:So if you have any other Nasal related questions, please let me know - I don't mind answering them in excruciating detail as long you improve our docs then :-)



Sure, and I'll see what I can do to improve the docs. ;)

Hooray wrote in Fri Dec 23, 2011 11:29 am:So, if you find the time to review our Nasal articles (because Nasal coding seems to be what you are interested in), please let us know if there's anything that we should improve eventually.


Sure.

Actually, I find Nasal, to be honest, boring and hard. It does seem familiar as I am a C (not C++) programmer myself. I am interested in modelling and doing .xml files for an aeroplane. However, whether I find it interesting or not does not matter, what matters is that there are people like you who answer questions for noobs like me. And for that, I thank you very much.

Also, to show you my kindness, I added a resources bit to the page, it's not much but just to show a token of my appreciation.

http://wiki.flightgear.org/Namespaces_a ... #Resources

Cheers,
Rupy
Rupert
 
Posts: 39
Joined: Fri Dec 02, 2011 4:21 am

Re: What are name spaces and methods?

Postby Hooray » Fri Dec 23, 2011 12:53 pm

You could have mentioned that you already know C. Obviously, C doesn't have namespaces like C++ does, but many concepts of C++ or Nasal could be compared to their C equivalents.

For example, a Nasal hash can be compared to a C struct, just with support for member functions and instance specific state.
In the same sense, members of a struct in C can be directly accessed (no access restrictions like private/protected), but they need to be fully qualified when accessed externally: struct.field vs. hash.field

In C++, you can also embed functions in structs:

Code: Select all
struct {
 const char* msg = "Hello");
 void hello() {
 printf("%s\n",msg);
 }
} house;



Once functions are embedded in a struct, you differentiate between functions that are basically "static", because they are not specific to a certain instance of the class (usually, helpers like conversion routines and such), and those that are instance-specific (and work with object state), the latter functions are called "methods".

Someone already familiar with C should Nasal find fairly simple actually. I am not sure what you find hard about it. Care to elaborate? I am not sure what you find boring about it, is it the language itself or what you can do with it in FG?

I guess we can provide better support here if you could elaborate a bit.

The truth is, you won't need any Nasal knowledge if all you are interested in is 3D modeling or creating your very first aircraft.
While Nasal knowledge can be useful, there are many aircraft developers who get by by copying code from other airplanes, that's also part of the reason why there is a huge and growing library of Nasal code.

From a programming point of view, any somewhat seasoned C programmer should find Nasal fairly familiar and easy, and indeed more abstract and also more powerful for high level programming than C itself. Some of Nasal's features may seem more familiar to people with OOP experience though, such as C++ programmers - this certainly applies to namespaces and methods, because these are high level concepts that need to "manually emulated" using low level C techniques (malloc/free, structs, function pointers in structs).
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11329
Joined: Tue Mar 25, 2008 8:40 am

Re: What are name spaces and methods?

Postby Rupert » Sat Dec 24, 2011 12:11 pm

Sorry about not mentioning that. :/

Next, I am still learning C so that may be why I sometimes have no idea what you are talking about.

Finally, if it weren't for your examples, I would not have a clue what namespaces and methods were.

Cheers,
Vini
Rupert
 
Posts: 39
Joined: Fri Dec 02, 2011 4:21 am

Re: What are name spaces and methods?

Postby Hooray » Sat Dec 24, 2011 12:28 pm

a "class" is really just a template for a certain data type that consists of other data types and provides functions to work with the class. The functions publicly accessible are called its "interface" because these functions are meant to be used by the users of your class. This is in contrast to member fields which are usually not meant to be accessed directly.

Once a class is used to create an object, we say the class is getting "instantiated", i.e. an instance of the class (an actual object) is created. This makes it then possible to actually make use of its interface and access member functions (which are methods).

Imagine a hash called "child":

Code: Select all
 var child = {};


In order to create new objects of type "child", we need to populate the parents vector, set to "child", this can be done conveniently by adding a new function named "new", of type func, that returns an anonymous hash with parents set to [child].

Code: Select all
 var child = {
  new: func {return {parents:[child]}; }
 };


Let's say, because it's christmas time, we want each child to be able to call home, so we simply add another method "callhome":

Code: Select all
 var child = {
  new: func {return {parents:[child]}; },
  callhome: {print("Calling home"); }
 };


in order to actually call a specific telephone number, we need a new field (variable to hold the number), this adds a new field named "number" to the constructor:


Code: Select all
 var child = {
  new: func {return {parents:[child],number:'1234'}; },
  callhome: {print("Calling home"); }
 };


Next, we need to change the callhome method, so that it actually shows the instance specific number, this is accomplished by using the "me" reference, which makes sure that we are referring to a variable that is specific to the currently executing context (i.e. object):

Code: Select all
 var child = {
  new: func {return {parents:[child],number:'1234'}; },
  callhome: {print("Calling home:",me.number); }
 };


Obviously, in its current form, the number will be identical for all created object, because the constructor always initializes the number field to the same 1234 value, but we could change the constructor to accept a telephone number argument instead:

Code: Select all
 var child = {
  new: func(tel) {return {parents:[child],number:tel}; },
  callhome: {print("Calling home:",me.number); }
 };


So, whenever we make a constructor call now, we need to pass in an argument for the telephone number to call:

Code: Select all
 var kid=child.new("123456");
 kid.callhome();


Now, in order to create 1000 different child objects, all we need to do is:

Code: Select all
 var children = [];
 for (var i=0; i<1000;i+=1) {
  append(children[i], child.new(i) );
}


Note that this will use the creation index as the phone number, also I didn't really test any of this - so it's possible that I made some typos somewhere :-)
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11329
Joined: Tue Mar 25, 2008 8:40 am


Return to Nasal

Who is online

Users browsing this forum: No registered users and 8 guests