OK, then it leads to another question - if modules are objects, why I can't modify variables, defined in those modules (directly)? And what _exactly_ happening when I import or inherit the module (the docs are a bit short in this topic)?
I'm not really sure. ;)
I don't know if there is a difference between inheriting objects and inheriting the object_program(object). Ie, you will inherit the objects program and thus get your own set of variables.
Module variables can be modified, but are assumed to be constants at compile-time. Ie,
int x=17; ... x=42; // at a later stage
yourmodule.x will here be 17 in some compilations and 42 in later. And never change where you use it outside the module.
int main() { Global_init(); write("Global's value: %O\n", Global->method()); }
See above. Modules are just there to give namespace. If you really want to do this (and have a global object), you need a query method to get the global object every time.
Ie,
object TheGlobal() { return Global; } ... write("Global's value: %O\n", TheGlobal()->method());
There is no use in accessing variables in modules from outside the module.
You *can* however instantiate the Global object when the module is created, like this:
object Global=GlobalClass();
and use it as a *constant*.
/ Mirar
Previous text:
2004-02-02 10:30: Subject: Re: Pike module initialization at compile time
On Mon, Feb 02, 2004 at 10:05:01AM +0100, Mirar @ Pike developers forum wrote:
- If you inherit modules, you're probably doing something that you really didn't want to do, since modules are objects, not classes.
OK, then it leads to another question - if modules are objects, why I can't modify variables, defined in those modules (directly)? And what _exactly_ happening when I import or inherit the module (the docs are a bit short in this topic)?
Rename Parent.pmod to Parent.pike if you really want to inherit it. .pike files represent classes (programs).
OK, this works - thank you :)
But there is another question, then...
I've a module with "global" object, initialized to 0. When I try to compile some file, which imports this module, I get an error:
=> Global.pmod <= GlobalClass Global;
class GlobalClass { string method() { return "I am Global!"; } }
void Global_init() { Global = GlobalClass(); }
=> x.pike <= import Global;
int main() { Global_init(); write("Global's value: %O\n", Global->method()); } ============
So, attempt to compile & run: "pike -M. x.pike" gives:
x.pike:7:Indexing on illegal type. (This is Global->method()) Pike: Failed to compile script: Compilation failed.
Well... Frankly, I see no reason why it cannot be _compiled_... When Global is initialized in place (...Global = GlobalClass()), everything is OK.
Regards, /Al
/ Brevbäraren