From: Edgar Friendly <thelema314@gmail.com>
> With a bit of low-level support, I imagine it not difficult to implement
> the following:
>
> type row = private int constraint (fun i -> i >= 0)
>
> such that the compiler uses the provided constraint function to check
> any (x :> row) casts, throwing an exception(?) on false. This solution
> wouldn't involve the module system just to have positive integer types,
> and gets rid of the function call overhead on 'from'.
This syntax could be nice, but it is just syntactic sugar for
module Private_row : sig
type row = private int
val new_row : int -> row
end = struct
type row = int
let new_row i = assert (i >= 0); i
end
include Private_row
I'm sure camlp4 can do that.
Direct compiler support couldn't give you more:
* you cannot use a coercion to create a row: coercions are purely
type-level features, and cannot execute any code; we don't want to
change this. On the other hand coercing row to int could be made ok.
* the "constraint ..." part cannot appear in an interface, since
interfaces cannot contain expressions
Changing any of these two would be difficult indeed.
(To be honest, the above results in
module Private_row : sig type row = private int val new_row : int -> row end
type row = Private_row.row
val new_row : int -> row = <fun>
meaning that Private_row is not completely hidden, eventhough we don't
need to mention it in an interface.)
Jacques Garrigue