Skip to content

Local exceptions (without the optimization)#301

Merged
alainfrisch merged 1 commit intoocaml:trunkfrom
alainfrisch:let_exception
Mar 15, 2016
Merged

Local exceptions (without the optimization)#301
alainfrisch merged 1 commit intoocaml:trunkfrom
alainfrisch:let_exception

Conversation

@alainfrisch
Copy link
Copy Markdown
Contributor

This is a stripped down version of #260. Here I only propose to add local exception definitions, with no specific compilation (which can come later or not).

Local exceptions should be encouraged, since they document a more limited scope, which helps reasoning about the code and avoid subtle bug when higher-order function use a generic exception (such as Exit) for their internal control flow but can easily get confused if they call unknown function which could also raise the same exception.

Moreover, now that it is no longer allowed to define multiple exceptions with the same name in a given structure, it becomes even more important not to pollute the global scope.

There is of course a work-around for encoding local exceptions with local modules, but it's syntactically heavier (two names need to be invented) and less efficient (unless more optimization are added). People would more be likely to use global exceptions if we don't provide something lighter than this encoding.

See https://blogs.janestreet.com/a-universal-type/ for some fun use of (previously encoded) local exceptions that would benefit from this feature:

module type Univ = sig 
  type t 
  val embed: unit -> ('a -> t) * (t -> 'a option) 
end 

module U : Univ = struct
  type t = exn

  let embed (type s) () =
    let exception E of s in
   (fun x -> E x), (function E x -> Some x | _ -> None)
end

Note: the grammar has been massaged a little bit to avoid a conflict with exception patterns:

let exception Exit = ... in ...
let exception Exit in ...

As a result, exception patterns are now disallowed in let bindings. They were rejected by the type-checker anyway.

@damiendoligez
Copy link
Copy Markdown
Member

FTR, I'm strongly in favour of the language change, but I don't feel qualified to review the implementation in detail.

@alainfrisch
Copy link
Copy Markdown
Contributor Author

I'm strongly in favour of the language change

Including for 4.03?

@damiendoligez
Copy link
Copy Markdown
Member

Including for 4.03?

We don't have a good consensus yet. What are the drawbacks of delaying it to 4.04?

@alainfrisch
Copy link
Copy Markdown
Contributor Author

What are the drawbacks of delaying it to 4.04?

Just extra work to maintain the list of active open PR and go back to it later. I don't think it would be technically difficult to maintain the branch synchronized with trunk.

@yakobowski
Copy link
Copy Markdown
Contributor

What are the drawbacks of delaying it to 4.04?

People using old versions of OCaml will get even less chance to use this new syntax before dying of old age? 😎

@damiendoligez damiendoligez added this to the 4.04-or-later milestone Jan 22, 2016
@alainfrisch
Copy link
Copy Markdown
Contributor Author

Rebased to trunk.

From the comments above, @damiendoligez is strongly in favor, and I suspect @yakobowski as well. Is anyone opposed to merging to trunk?

@damiendoligez
Copy link
Copy Markdown
Member

IIRC, the only opposition was from @lefessan, everyone else liked it.

Can you find someone to review the code?

@alainfrisch
Copy link
Copy Markdown
Contributor Author

@garrigue Do you think you could have a look at the type-checking part of this PR? (This is less than 10 lines, the rest is syntactic boilerplate or moving code around.)

@garrigue
Copy link
Copy Markdown
Contributor

This looks fine to me. But some tests would help confidence.

@alainfrisch alainfrisch force-pushed the let_exception branch 4 times, most recently from 726b5a7 to c7df852 Compare March 15, 2016 15:40
@alainfrisch
Copy link
Copy Markdown
Contributor Author

Now complete with a non-regression test, an entry in Changes and in the manual.

alainfrisch added a commit that referenced this pull request Mar 15, 2016
Local exceptions (without the optimization)
@alainfrisch alainfrisch merged commit b791a20 into ocaml:trunk Mar 15, 2016
poechsel pushed a commit to poechsel/ocaml that referenced this pull request Jan 25, 2021
chambart pushed a commit to chambart/ocaml-1 that referenced this pull request Jan 4, 2022
* Revert "Use polymorphic compare for [Arch.equal_*] (ocaml#293)"

This reverts commit a4cd7b7.

* Simplify the case analysis when the arguments are not equal

and still have warning 4 on.

* Update equal_specific_operation with recently added operations
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants