I would like to access a __gshared variable from a function. The code snippet is like this:
import vibe.d;
struct Student
{
string name;
uint number;
}
class StudentStore
{
Student[][string] store;
static Student[0] empty;
Student[] getNotes(string id)
{
return (id in store) ? store[id] : empty;
}
private __gshared StudentStore studentStore;
shared static this()
{
studentStore = new StudentStore();
}
}
void listStudents(HTTPServerRequest req, HTTPServerResponse res)
{
auto allstudents = studentStore.getNotes(req.session.id);
}
void main()
{
auto router = new URLRouter;
router.get("/", staticTemplate!"index.dt");
router.get("*", serveStaticFiles("public/"));
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
listenHTTP(settings, router);
logInfo("Lütfen tarayıcınızda http://127.0.0.1:8080/ adresini açınız.");
runApplication();
}
As I know, there are two ways to make a variable available across threads in D: __gshared and shared .
The former has no guarantees. All it does is put the variable in global storage, making it just like any global variable in C or C++.
The latter actually affects the type of the variable; the compiler is able to help prevent the variable from being used in ways it shouldn't be.
When I try to access __gshared variable studentStore, I get undefined identifier error.
source/app.d(26,24): Error: undefined identifier studentStore, did you mean class StudentStore?
What might be the cause of this?