Skip to content

Commit

Permalink
Use a polymorphic variant result type
Browse files Browse the repository at this point in the history
This allows us to easily use the same result type from other libraries,
without them being different types, needing different binds etc.

Signed-off-by: David Scott <[email protected]>
  • Loading branch information
David Scott committed Aug 18, 2014
1 parent 57df5ce commit 58471e5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v0.2 (unreleased)
* add Mbr_partition: V1_LWT.BLOCK, for easy access to partitions via
the standard Mirage block interface.

* use a polymorphic variant result type [`Ok of 'a | `Error of 'b]
4 changes: 2 additions & 2 deletions cli/impl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ let info common filename =
Lwt_unix.openfile filename [ Lwt_unix.O_RDONLY ] 0o0 >>= fun fd ->
Mbr_lwt.really_read fd mbr >>= fun () ->
let mbr = match Mbr.unmarshal mbr with
| Mbr.Error reason ->
| `Error reason ->
Printf.fprintf stderr "Failed to unmarshal MBR: %s\n%!" reason;
exit 1
| Mbr.Ok x -> x in
| `Ok x -> x in
let all = List.map (fun f ->
match Mbr.get mbr f with
| Some v -> [ f; v ]
Expand Down
15 changes: 8 additions & 7 deletions lib/mbr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

type ('a, 'b) result =
| Ok of 'a
| Error of 'b
type ('a, 'b) result = [
| `Ok of 'a
| `Error of 'b
]

let ( >>= ) x f = match x with
| Error y -> Error y
| Ok z -> f z
| `Error y -> `Error y
| `Ok z -> f z

let return x = Ok x
let fail y = Error y
let return x = `Ok x
let fail y = `Error y

let kib = 1024L
let mib = Int64.mul kib 1024L
Expand Down
7 changes: 4 additions & 3 deletions lib/mbr.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

type ('a, 'b) result =
| Ok of 'a
| Error of 'b
type ('a, 'b) result = [
| `Ok of 'a
| `Error of 'b
]

module Geometry : sig
type t = {
Expand Down

0 comments on commit 58471e5

Please sign in to comment.