BetterErrors icon indicating copy to clipboard operation
BetterErrors copied to clipboard

Error: The type of this expression... contains type variables that cannot be generalized

Open bkase opened this issue 9 years ago • 2 comments

Hi!

This is a great project, it's really helping me learn Reason

I came across this error:

wat.re:14 28-43
11 │
12 │ type c 'a = option (tuple 'a);
13 │
14 │ let optSwap: c 'a => c 'a = Option.map swap

Error: The type of this expression, '_a c -> '_a c,
       contains type variables that cannot be generalized

when I was doing something similar to this:

let module Option = {
  let map = fun f =>
    fun
    | None => None
    | Some x => Some (f x);
};
type pair 'a = | Pair of 'a 'a;
let swap =
  fun
  | Pair x y => Pair y x;

type c 'a = option (pair 'a);

let optSwap: c 'a => c 'a = Option.map swap

This page shows a very simple repro case: https://ocaml.org/learn/tutorials/common_error_messages.html#Thetypeofthisexpressioncontainstypevariablesthatcannotbegeneralized

I understand why ref None needs more type information, but why does Option.map swap? I feel like the language should let me do this:

wat.re:17 28-31
14 │ let optSwap: c 'a => c 'a = Option.map swap;
15 │
16 │ let _ = optSwap (Some (Pair 1 2));
17 │ let _ = optSwap (Some (Pair 'a' 'b'));

Error: The types don't match.
This is: char
Wanted:  int

In any case, this probably deserves a better error, right?

bkase avatar Aug 21 '16 23:08 bkase

@bkase generalization doesn't survive through partial application afaik. you need to define a new function like

let optSwap val => Option.map swap val

jaredly avatar Aug 31 '16 19:08 jaredly

Merging #36 into here!

https://ocaml.org/learn/tutorials/common_error_messages.html

See the relevant section down there.

chenglou avatar Sep 06 '16 23:09 chenglou