Browse thread
[Caml-list] Some/None
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Oliver Bandel <oliver@f...> |
| Subject: | Re: [Caml-list] Some/None |
On Thu, 25 Apr 2002, John Max Skaller wrote:
> Oliver Bandel wrote:
>
> >Does it make sense to give back None/Some
> >out of a function
> >
> Oh yes! I do this all the time, lots and lots.
>
> Consider
>
[...]
> which searches a list for a number x,
> and returns its position n as
>
> Some n
>
> if it is found in the list, or
>
> None
>
> if it isn't: its position in the list is None,
> it doesn'tr have a position, or Some position
> if it does.
OK, that makes sense to me. :)
>
> >(e.g. is_regularfile/is_directory/...)
> >
>
> What you want here is:
>
> type file_type_t = Regular | Directory | Special | Root | NonExistant
Wow!
Good idea. So I don't rely on the predefined type and use my own :)
That's fine.
>
> let file_type (s:string): file_type_t =
>
> (code to find the file type here) ...
OK :)
let type_of_file (name:string) =
try (
match (Unix.stat name).Unix.st_kind
with
| Unix.S_REG -> Regular
| Unix.S_DIR -> Directory
| Unix.S_CHR -> Special
| Unix.S_BLK -> Special
| Unix.S_LNK -> Special (* ok, on ENOENT Unix.lstat *)
| Unix.S_FIFO -> Special
| Unix.S_SOCK -> Special
)
with
| Unix.Unix_error (Unix.ENOENT,_,_) -> NonExistant (* maybe link-problem => Unix.lstat *)
So this makes completely sense.
[...]
> type file_data =
> | Regular of int (* file size in megs *)
> | Directory of int (* number of entries *)
> | Special
> ....
nice idea. :)
Thanks,
Oliver
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners