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

Comments

Projects
None yet
1 participant
@vicuna
Copy link
Collaborator

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

This comment has been minimized.

Copy link
Collaborator 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

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 2, 2014

Comment author: @yallop

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

@vicuna

This comment has been minimized.

Copy link
Collaborator 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

@vicuna vicuna closed this Dec 11, 2015

@vicuna vicuna added the typing label Mar 14, 2019

@vicuna vicuna added the feature-wish label Mar 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.