Namespaces with Include Files

When I write complex JSL applications I’ve tended to use named namespaces so that they are easy to consistently reference across include files.  But this has some limitations.  I am building a framework that will underpin multiple applications and so it is important that I use unique names for the namespaces.  The easiest way to do that is to use anonymous namespaces.  But there seems to be some strange behaviour …

To investigate the behaviour of namespaces used across include files I will construct a simple scenario.  I have a main program called Main1.jsl.

When I click the button the function DoSomethingForAWhile is invoked.  This is defined in the include file:

When I run the main program the log window shows the name of the anonymous namespace e.g. “#65”.

When I click the button in the test window I get output like this:

So far so good.  The namespace ns defined by the main program appears to be accessible by the include file (as illustrated by the creation of the variable namespaceName.

Instead of using the loop variable to create the count, let me introduce an explicit count variable that is contained within the namespace:

When I run the code now I get the following error:

namespace-error

 

 

 

Is it really  not defined?  How  was I able to send a message to it and get the correct name?

Let’s try interrogating the ns variable in more detail:

show properties(ns) reveals:

These are the properties I expect to see for a namespace.  And usually you can inspect the values contained inside the namespace like this:  show(ns).  But this just reveals:

Which is usually a hint from JMP that the variable is undefined.

So what is going on?  A namespace structurally is an associative array.  Let’s see if I can inspect the contents of the namespace treating it like an array.  But first, I will add the following line to the main program, at the point where ns is still recognised as a namespace:

Now at the beginning of my include function I can try:

show(ns << Get Keys)

This reveals:

So where are we.  From within the include file we have a variable ns which is preserving the contents of the original namespace defined in the main program.  The variable can be sent messages but it can not be referenced using the usual colon notation.  JMP also has an IsNamespace function and using this confirms that ns is a namespace.

This appears to be a classic type of “type casting” problem associated with programming languages that require explicit data type declarations.

JMP has a number of “As” functions that allow variables to be used “as” a particular type of variable.

How the code works as intended, correctly identifying the count variable and namespace name (which due to all the testing has now moved on to #79!):

Everything is looking good, but there is one more important apect to investigate.  I have a second program, Main2.  It’s identical to Main1 and allows be to run 2 programs in parallel.

Importantly, both programs use the same include file but when running they preserve their own instance of the namespace referenced with the common variable name ns.

Share the joy:
facebooktwittergoogle_pluslinkedinmailfacebooktwittergoogle_pluslinkedinmail

One thought on “Namespaces with Include Files”

Leave a Reply