Advertisement
tinyevil

Untitled

Feb 19th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. linear type File
  2. { handle : i32 }
  3.  
  4. --a private type, "evidence" for file state
  5. adt Opened<File f>{ c_opened :: Opened<f> }
  6. adt Closed<File f>{ c_closed :: Closed<f> }
  7.  
  8. static new :: () -> (f::File, Closed f)
  9.  
  10. open :: Path -> Either (f::File, Closed f, Error) (f::File, Opened f)
  11.  
  12. read :: Opened this -> int -> Either (f::File , Closed f, Error) (f::File, Opened f, Bytes)
  13. write :: Opened this -> Bytes -> Either (f::File, Closed f, Error) (f::File, Opened f)
  14.  
  15. close :: Opened this -> (f::File, Closed f)
  16.  
  17.  
  18. File.new () =
  19. let
  20. f = file { InvalidHandle }
  21. in
  22. (f, c_closed f)
  23.  
  24. File.open this path =
  25. let h = fopen(path, "rw")
  26. in if h == 0
  27. then let f = File{h} in (Left f, c_closed f, "can't open the file")
  28. else (Right f, c_opened f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement