Skip to content

Global module variables #152

Answered by mcfriend99
benstigsen asked this question in Q&A
Discussion options

You must be logged in to vote

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 of data. In this light, its not possible to do what you are trying to do this way.

However, the builtin reflect module defines the function set_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:

import reflect

var data = {}

def get_data() {
  return data
}

reflect.set_global(get_data)

foo.b:

def init() {
  get_data()["hello"] = nil
}

bar.b:

def init() {
  echo g…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by benstigsen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants