errors/TypesDoNotUnify.md
TypesDoNotUnify Error> 1 == "test"
Error found:
in module $PSCI
at line 1, column 6 - line 1, column 6
Could not match type
String
with type
Int
while checking that type String
is at least as general as type Int
while checking that expression "test"
has type Int
This error occurs when two types are required to be equal, but the type checker is unable to verify that they are equal.
In the example above, the types String and Int can never be made equal, hence the error.
"test" has type Int".Unlike in JavaScript, all elements of a PureScript array must be the same type. Otherwise the types will fail unification when being matched by type inference. For example, [true, false] or [1, 2, 3] work fine but [1, false] does not.
f :: Int -> Int
f x = x + 1
g :: Boolean -> Boolean
g x = x || true
h = g <<< f
The type of (<<<) (that is, function composition) is
forall a b c. (b -> c) -> (a -> b) -> (a -> c)
For the right hand side of h to type-check, we need to find types a, b, and c such that the types match up. That is, we need to find a choice of a, b, and c such that:
b = Boolean (from the argument type of g)c = Boolean (from the return type of g)a = Number (from the argument type of f)b = Number (from the return type of f).b can not be Boolean and Number at the same time, so this system of equations is not satisfiable, and the type checker rejects the program.
Note that for row types (e.g. in objects or effects) to be equal, not only the types, but the labels themselves must match identically.