Global module variables #152
-
So I have something like
import .foo
import .bar
foo.init()
echo foo.data.keys() # [hello]
bar.init()
var data = {}
def init() {
data["hello"] = nil
}
import .foo
def init() {
echo foo.data.keys() # []
} What I would expect to see:
What I see instead:
Is there any way to have foo.init()
bar.init()
bar.data = foo.data |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In Blade, imported module/packages are local to the file/module that imported them so every import of However, the builtin
import reflect
var data = {}
def get_data() {
return data
}
reflect.set_global(get_data)
def init() {
get_data()["hello"] = nil
}
def init() {
echo get_data().keys() # []
}
import .base
import .foo
import .bar
foo.init()
echo get_data().keys() # [hello]
bar.init() In this example, the base module defines the global value data and makes it available via the function You can always retrieve an instance of data anywhere in your application using the |
Beta Was this translation helpful? Give feedback.
In Blade, imported module/packages are local to the file/module that imported them so every import of
foo
module will create a new instance ofdata
. In this light, its not possible to do what you are trying to do this way.However, the builtin
reflect
module defines the functionset_global
that allows you set a function or class as globally available throughout your application. So to answer the question "Is there any way to have foo.data be completely global to all of them?
", I would suggest something like this.base.b
:foo.b
:bar.b
: