ref: b14b8d7f7795dc29dcd5e1a885f21cf40fca55ba
parent: 28844433e49e746b81360a3a8d544b155d763095
author: Ori Bernstein <[email protected]>
date: Wed Oct 9 11:13:32 EDT 2013
Return optional types from constructors.
--- a/bio.myr
+++ b/bio.myr
@@ -23,8 +23,8 @@
/* creation */
const mkfile : (fd : std.fd, mode : mode -> file#)
- const open : (path : byte[:], mode : mode -> file#)
- const create : (path : byte[:], mode : mode, perm : int -> file#)
+ const open : (path : byte[:], mode : mode -> std.option(file#))
+ const create : (path : byte[:], mode : mode, perm : int -> std.option(file#))
const close : (f : file# -> bool)
/* basic i/o. Returns sub-buffer when applicable. */
@@ -85,6 +85,7 @@
const create = {path, mode, perm
var openmode
+ var fd
if mode == Rd
openmode = std.Ordonly
@@ -95,7 +96,12 @@
;;
openmode |= std.Ocreat
- -> mkfile(std.open(path, openmode, perm castto(int64)), mode)
+ fd = std.open(path, openmode, perm castto(int64))
+ if fd < 0
+ -> `std.None
+ else
+ -> `std.Some mkfile(fd, mode)
+ ;;
}
const close = {f