tinyevil

Untitled

Feb 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 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. new :: () -> (f::File, Closed f)
  9.  
  10. open :: (this::File) -> Closed this -> Path -> Either (f::File, Closed f, Error) (f::File, Opened f)
  11.  
  12. read :: (this::File) -> Opened this -> int -> Either (f::File , Closed f, Error) (f::File, Opened f, Bytes)
  13. write :: (this::File) -> Opened this -> Bytes -> Either (f::File, Closed f, Error) (f::File, Opened f)
  14.  
  15. close :: (this::File) -> Opened this -> (f::File, Closed f)
  16.  
  17. File.new () =
  18. let
  19. f = file { InvalidHandle }
  20. in
  21. (f, c_closed f)
  22.  
  23. File.open this ev_closed path =
  24. let h = fopen(path, "rw")
  25. in if h == 0
  26. then let f = File{h} in (Left f, c_closed f, "can't open the file")
  27. else (Right f, c_opened f)
Add Comment
Please, Sign In to add comment