Variables across different files


#1

I have two files (main.chai and timer.chai)

I’ve created a class inside timer.chai called TimerManager and one instance of it (all inside timer.chai).
with use(“timer.chai”) from inside main.chai I can see TimerManager class and create new instances of it, but I can’t actually access the instance I’ve created on the other file. Why does this happen?
How can I access variables from other files? How to make a singleton instance if needed?

Thanks!


#2

You want to use the add_global function to expose the variable outside of the current scope.

See also https://github.com/ChaiScript/ChaiScript/issues/122 and http://chaiscript.com/docs/5/classchaiscript_1_1ChaiScript.html#af5d3435e41f173b947badf7b3caeae7e

Jason


#3

I found that function later, seems to work fine. Thanks!

And btw, is there any plans to a base framework for the language? Just like the stdlib, but for things like file access, math for example, better string manipulation? I had to export floor/ceil functions to the language.

Maybe this could be a separate project?


#4

Regex support is on the back burner, and I want that to be part of the core. Otherwise, no direct plans to support more libraries.

Here’s something I could see being a huge help: a collection of header files that support subsets of functionality that users might want, like:

// math.hpp
namespace math_funcs {
  chaiscript::ModulePtr bootstrap(chaiscript::ModulePtr mod = std::make_shared<chaisript::ModulePtr>()) {
    mod.add(chaiscript::fun(std::floor), "floor");
    //...
  }
}

Then users could add these at will, and we could provide some helpers for pulling in / compiling multiple utilities.

I could see this being part of ChaiScript or living as a separate project. Like a ChaiScript_extras project.

If you want to get started on something like this, starting with the wrappers you have already written, I will support you on it.

-Jason


#5

I said a separate project because I don’t know how clean you want to keep the language and the main codebase.


#6

I’m mainly concerned about compile times more than anything else, but I think this gives us a very clean option, and it was a great idea.

Here’s the start:

And here’s the first file added:

For now this is very simple. Before I make the next release of ChaiScript I’ll put some form of automated testing in place for this code. Let me know what you think.

-Jason