E4050

E4050#

Definition cycle detected in dependencies.

The toplevel definitions in a MoonBit package are mutually recursive. This means that the definitions can refer to each other in any order. However, no cycles are allowed in the dependencies between the toplevel variable definitions.

Erroneous example#

pub let a : Int = f() // Error: Definition cycle detected : a -> f -> a

fn f() -> Int { // Error: Definition cycle detected : a -> f -> a
  a
}

Suggestion#

The fix to this error varies depending on the logic of the program. One possible fix is to make the variable definition to use Ref[_?], and initialize the variable later in init block of the program.

pub let a : Ref[Int?] = Ref::new(None)

fn init {
  a.val = Some(f())
}

fn f() -> Int {
  a.val.unwrap()
}