Type Checking
Array
In general JSON, Arrays can contain various values.
// In JSON, this is OK.
[1, 3.14, ["cumin"]]
But this is very strange and buggy. In cumin, Arrays should contain values with same type.
// In cumin, this is NG.
[1, 3.14, ["cumin"]]
This occurs and error
Error: Cannot infer type of Array([Nat(1), Float(3.14), Array(String, [Str("cumin")])]); Hint: Array cannot contain values with different types.
Any Type
Any is the top type.
let x: Any = 100;
This is ok. And don't worry. cuminc knows that x is Nat (because 100 is Nat).
Any is convenient in some cases.
This example is trivial, and it is equivalent to let statement without type annotation.
For example, Array<Any> is a type for Something Arrays.
let xs: Array<Any> = [
1, 2, 3
];
In this example,
- The elements are all
Nat. xsis a something arrayArray<_>.- These facts conclude that
xsisArray<Nat>.
Because _ is an alias for Any, you can write
let xs: Array<_> = [
1, 2, 3
];
It is unsafe that struct fields are declared as Any.
struct Data {
data: Any,
}
Since data can be any values, followings are all valid.
let x = Data {
data = 1, // Nat
};
let y = Data {
data = 3.14, // Float
};
let z = Data {
data = ["cumin"] // Array<String>
};
And cuminc knows only that x, y and z are just Data, and ignores the type of data.
So they have all same type!
Hack: Array with various data.
In cumin v0.9.7, This is ok.
struct Data {
data: Any,
}
let x = Data {
data = 1, // Nat
};
let y = Data {
data = 3.14, // Float
};
let z = Data {
data = ["cumin"] // Array<String>
};
[x, y, z]
Because the last data is just Array<Data>.
NOTE: No warrantry to support this hack.