The value restriction

前端 未结 3 1400
醉梦人生
醉梦人生 2020-11-28 14:36

In OCaml you can\'t generalize a partially-applied curried function (the \"value restriction\").

What is the purpose of the value restriction? What unpleasant would

3条回答
  •  误落风尘
    2020-11-28 14:57

    Here is the answer I gave some time ago about F#; the issue is exactly the same with OCaml. The problem is that without it, we would be able to create references that point to the wrong type of data:

    let f : 'a -> 'a option =
        let r = ref None in
        fun x ->
            let old = !r in
            r := Some x;
            old
    
    f 3           // r := Some 3; returns None : int option
    f "t"         // r := Some "t"; returns Some 3 : string option!!!
    

提交回复
热议问题