Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Function forward declaration?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Benjamin Geer <ben@s...>
Subject: Re: [Caml-list] Function forward declaration?
Richard Jones wrote:
> This is the sort of thing which I'd like to write.

It looks as if using exceptions would simplify your example.  If your 
check_permissions and find_resource functions threw exceptions 
containing error messages (e.g. Failure), you could write:

let run dbh q userid =
   try
     check_permissions userid;
     let query = q#param "query" in
       if is_empty query then
         failwith "Please enter a query"
       else
         let resource =
           try
             find_resource dbh query
           with Not_found ->
             failwith "That resource doesn't exist in the database"
         in
           (* do some work *);
           StdPages.ok q "OK, your query was executed"
   with Failure msg ->
     StdPages.error q msg ;;

Besides being more concise, I think this is an improvement because it 
separates the identification of an error with the way the error is 
reported.  This means that if someday you want to change the way you 
handle errors, you have less code to change; in the version above, there 
is only one line of code that knows about StdPages.error.

(It would perhaps be even better to wrap the calls to StdPages.ok and 
StdPages.error in more generic report_success and report_error 
functions, so that if someday you changed your mind about having a 
single standard error page, or if you decided to implement a completely 
different sort of GUI, e.g. using GTK+, you wouldn't have to change the 
'run' function at all.)

Ben

-------------------
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