E4074

E4074#

This error code is shared by the following two error messages:

  • Cannot infer the type of variable, please add more type annotation.

  • Public definition a must be annotated with its type.

When defining a toplevel variable using let, MoonBit will try to infer its type when the value is literal:

pub let forty_two = 42
pub let float = 1.0
pub let string = "Hello, world!"
pub let array = [1, 2, 3]

错误示例#

For “Cannot infer the type of variable a, please add more type annotation”:

let a = 1 + 1 // Error: Cannot infer the type of variable a, please add more type annotation.

The other error message is more subtle. Say if you have module username/hello and package a:

a/a.mbt:

pub let value = 1.0

And package b:

b/moon.pkg.json:

{
  "import": [
    "username/hello/a"
  ]
}

b/b.mbt:

pub let value = @a.value // Error: Public definition a must be annotated with its type.

建议#

These two error can be fixed by explicitly annotating the type of the variable:

let a : Int = 1 + 1
pub let value : Double = @a.value