Loading ...
Sorry, an error occurred while loading the content.
 

Re: "ocaml_beginners"::[] Toplevel scope question

Expand Messages
  • Richard Jones
    ... All you can do is to restart the toplevel. However a partial solution is to redefine the variable with some unlikely type, eg: let x = `Undefined ;; That
    Message 1 of 2 , Dec 6, 2008
      On Sat, Dec 06, 2008 at 03:10:31AM -0000, Zach wrote:
      > I was working on a function that referenced a variable without first
      > defining it. It took me a long time to find this error, because at
      > some point during my interactive testing at the toplevel, I had
      > defined a variable with the same name. Is there a way to get the
      > compiler to give you an error in this case? I know the whole
      > interactive toplevel testing thing depends on this feature so you can
      > access functions at the toplevel, but can you disable it for
      > variables? Or at the very least, can you undefine symbols that are
      > already defined at the toplevel?

      All you can do is to restart the toplevel.

      However a partial solution is to redefine the variable with some
      unlikely type, eg:

      let x = `Undefined ;;

      That gives x a type [> `Undefined] which is unlikely to type-check if
      x is subsequently used in another expression:

      # let add y = x + y ;;
      -
      Error: This expression has type [> `Undefined ] but is here used with type
      int

      You could wrap this in a camlp4 macro so that writing 'undefine x' is
      the same as the above ... Not really clear if this is useful, since
      most likely you'd want to undefine everything, in which case just
      restart the toplevel.

      Rich.

      --
      Richard Jones
      Red Hat
    Your message has been successfully submitted and would be delivered to recipients shortly.