`non-static expression in library or module`. What's wrong?

Trying to compile:

actor CanDBIndex {
  stable var owners: [Principal] = [];

I get
non-static expression in library or module. What’s wrong?

The following does not work, too:

actor CanDBIndex {
  stable var owners: ?[Principal] = null;

Those should be fine, but is that the complete code?

I expect you might be importing a module that does some computation rather than being a simple collection of functions and values.

Imported modules must be pure, i.e. have no side effects, which we approximate by applying a syntactic restriction to imports. For example, a library cannot contain a top level Debug.print, var declaration, or other side-effecting code.

Ah, are you trying to import those actors into other code? That’s not allowed using a regular library import, since actors are not considered static (i.e. pure). You should only import an actor through an alias to a separately compiled canister.

I think, the problem can be solved by using

import B "canister:B";

instead of

import B "B";
1 Like