Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] there has to be a better way to write this code
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Hecker <checker@d...>
Subject: [Caml-list] there has to be a better way to write this code

I'm trying to write a little property system, which means I have a
dictionary/hash with strings for keys and a variant of different types
for the data.  Then, I define get_float and get_string and the like on
this hash, and the functions either return the type if it exists, or
throw an exception if it doesn't.  The attached code below works fine.

My problem is that the attached code is really pretty verbose and
redundant.  Each of the get_* functions has a lot of duplicate code in
it, and I think there must be a better way.  I'd like to write
something like this (not really caml syntax, but you get the idea):

let get_float h s = get_meta h s (Float el -> el)

but I can't see any way to get that to work.  The closest I can come
is this:

let get_meta h s f =
  try
    List.iter f (Hashtbl.find_all h s);
    raise Not_found
  with Match_failure _ -> raise Not_found

exception Found_float of float

let get_float h s =
  try
    get_meta h s (function Float el -> raise (Found_float el))
  with Found_float el -> el

which isn't much of a savings over the code below, and probably isn't
worth it.  (Have I mentioned how annoying that exception at global
scope is? :)

If I didn't want to have multiple types per key, I could do this:

let get_float h s =
  match Hashtbl.find h s with
    Float el -> el
  | _ -> raise Not_found

which is slightly better, but having the multiple types is nice
(especially since Hashtbl supports it).

Thoughts?

Chris


----------------------------------------
type data_type =
    String of string
  | Float of float
  | Vec of (float * float)
  | Bool of bool

let h = Hashtbl.create 7

let _ =
  Hashtbl.add h "foo" (String "foo_string");
  Hashtbl.add h "foof" (Float 3.141);
  Hashtbl.add h "bar" (String "bar_string");
  Hashtbl.add h "bar" (Float 2.718);
  Hashtbl.add h "vec" (Vec (1.0,2.0));
  Hashtbl.add h "yes" (Bool true);
  Hashtbl.add h "no" (Bool false)
    
exception Found_float of float
exception Found_string of string
exception Found_vec of (float * float)
exception Found_bool of bool
    
let get_float h s =
  try
    List.iter (function Float el -> raise (Found_float el) | _ -> ()) (Hashtbl.find_all h s);
    raise Not_found
  with Found_float el -> el

let get_string h s =
  try
    List.iter (function String el -> raise (Found_string el) | _ -> ()) (Hashtbl.find_all h s);
    raise Not_found
  with Found_string el -> el

let get_vec h s =
  try
    List.iter (function Vec el -> raise (Found_vec el) | _ -> ()) (Hashtbl.find_all h s);
    raise Not_found
  with Found_vec el -> el

let get_bool h s =
  try
    List.iter (function Bool el -> raise (Found_bool el) | _ -> ()) (Hashtbl.find_all h s);
    raise Not_found
  with Found_bool el -> el


-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr