Skip to content

Latest commit

 

History

History
78 lines (58 loc) · 1.27 KB

5017.md

File metadata and controls

78 lines (58 loc) · 1.27 KB

5017

A type escapes the scope in which it is valid.

Here, the type d is only available for the scope of the let expression, but the let expression would cause a value of type d to "escape" the let and be bound to x, outside the let.

val x =
    let datatype d = D in D end
(** ^^^ type escapes its scope: `d` *)

This can also occur for explicit type variables, as in this example:

fun f b x =
(** + type escapes its scope: `'a` *)
  let
    fun g (y : 'a) = if b then x else y
  in
    ()
  end

Here, x and y are both inferred to have type 'a. Note that:

  • 'a is implicitly bound at fun g.
  • x is bound before fun g.

To fix

Try one of the following:

  • Extend the scope of the type:

    datatype d = D
    val x = D
  • Do not allow its values to escape its scope:

    val x =
      let
        datatype d = D
      in
        4
      end
  • Remove explicit type variable annotations:

    fun f b x =
      let
        fun g y = if b then x else y
      in
        ()
      end
  • Change where type variables are bound:

    fun 'a f b x =
      let
        fun g (y : 'a) = if b then x else y
      in
        ()
      end