Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Existential types for exceptions #6219

Closed
vicuna opened this issue Nov 2, 2013 · 3 comments
Closed

Existential types for exceptions #6219

vicuna opened this issue Nov 2, 2013 · 3 comments

Comments

@vicuna
Copy link

vicuna commented Nov 2, 2013

Original bug ID: 6219
Reporter: @yallop
Assigned to: @yallop
Status: closed (set by @xavierleroy on 2015-12-11T18:26:48Z)
Resolution: fixed
Priority: normal
Severity: feature
Fixed in version: 4.02.0+dev
Category: typing
Related to: #5584
Monitored by: @gasche @diml @hcarty @alainfrisch

Bug description

It would be useful for exceptions to support existential types via GADT-style declarations:

exception E : 'a t -> exn

@vicuna
Copy link
Author

vicuna commented Nov 4, 2013

Comment author: @alainfrisch

Side note: the trunk version syntactically allows the suggested form, which is then rejected by the type-checker.

@vicuna
Copy link
Author

vicuna commented Apr 2, 2014

Comment author: @yallop

This will be supported naturally if exceptions are subsumed by open types.

@vicuna
Copy link
Author

vicuna commented May 5, 2014

Comment author: @yallop

This has been resolved by PR5584. Here's an example use, which now works in trunk:

module type INT =
sig
type t
val to_string : t -> string
val div : t -> t -> t
val zero : t
end

type 'i intmod = (module INT with type t = 'i)

exception Division_by_zero : 'a intmod * 'a -> exn

let div : type i. i intmod -> i -> i -> i =
fun ((module I) as i) n d ->
if d = I.zero then raise (Division_by_zero (i, n))
else I.div n d

let f (type i) ((module I : INT with type t = i) as i) x y =
match div i x y with
z ->
Printf.printf "%s / %s => %s\n"
(I.to_string x) (I.to_string y) (I.to_string z)
| exception Division_by_zero ((module I), n) ->
Printf.printf "%s / 0 => ERROR\n" (I.to_string n)

(* prints "12 / 4 => 3" *)
let _ = f (module Int32) 12l 4l

(* prints "12 / 0 => ERROR" *)
let _ = f (module Int64) 12L 0L

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant