E1013#
The type of this expression contains unresolved type variables.
This error occurs when the compiler encounters a type that contains type variables which cannot be determined from the context. Type variables are placeholders for types that should be inferred by the compiler.
Common cases where this happens:
Writing local functions without explicit type annotations
Creating empty collections (Arrays, Options) without specifying their element type
When the compiler cannot resolve these type variables, it defaults them to
Unit
type, which may not be what you intended.
Erroneous example#
pub fn f1() -> Unit {
fn f(x) {
// ^ Warning: The type of this expression is Option[_/0], which contains
// unresolved type variables. The type variable is default to
// Unit.
match x {
None => ()
Some(a) => println(a)
}
}
f(None)
}
pub fn f2() -> Unit {
fn f(x : Array[_]) -> Int {
// ^ Warning: The type of this expression is Array[_/0], which contains
// unresolved type variables. The type variable is default to
// Unit.
x.length()
}
println(f([]))
}
fn main {
let a = []
// ^^ Warning: The type of this expression is Array[_/0], which contains
// unresolved type variables. The type variable is default to Unit.
println(a.length())
let b = None
// ^^ Warning: The type of this expression is Option[_/0], which contains
// unresolved type variables. The type variable is default to Unit.
println(b.is_empty())
}
Suggestion#
To fix this warning, you can:
Add type annotations to local function parameters. For example,
pub fn f1() -> Unit {
fn f(x: Option[Int]) {
match x {
None => ()
Some(a) => println(a)
}
}
f(None)
}
pub fn f2() -> Unit {
fn f(x : Array[Int]) -> Int {
x.length()
}
println(f([]))
}
Explicitly specify the type of the variable or the collection element type.
fn main {
let a : Array[Int] = []
println(a.length())
let b : Option[Int] = None
println(b.is_empty())
}
Or equivalently, add annotations on the collection creation.
fn main {
let a = ([] : Array[Int])
println(a.length())
let b = (None : Option[Int])
println(b.is_empty())
}
Provide enough context through usage.
pub fn f1() -> Unit {
fn f(x) {
match x {
None => ()
Some(a) => println(a + 1)
// ^^^^^ through this usage, the compiler can infer the
// type of `x` is `Option[Int]`.
}
}
f(None)
}