From: Jesse G. <je...@wi...> - 2004-08-14 16:08:01
|
Nicolas Cannasse wrote: >> Would extLib be interested in a general purpose Environment >> interaction module? Here's what I have fully implemented >> so far: >> >> (** Various functions and classes for interacting with the environment >> *) >> >> (** {1 Functions for interacting with the current process environment } >> *) >> >> val get : string -> string >> val strict_get : string -> string >> val default : ?def:string -> string -> string >> val put : string -> string -> unit > > What are differences between default, get, and strict_get ? > I think two functions would be enough : get is a wrapper for Unix.getenv default is Unix.getenv, but with a default return value instead of Not_found strict_get is Unix.getenv, but it also throws Not_found if the variable is an empty string "". > val get : string -> string option > val default : def:string -> env:string -> string > > >> (** {1 Class for interacting with a local fabricated environment } *) >> >> class env : >> ?env:string array -> >> ?size:int -> >> unit -> >> object >> val mutable env_h : (string, string) Hashtbl.t >> method environment : unit -> string array >> method fold : (string -> string -> 'a -> 'a) -> 'a -> 'a >> method get : string -> string >> method iter : (string -> string -> unit) -> unit >> method length : unit -> int >> method put : string -> string -> unit >> method remove : string -> unit >> end > > This is an object wrapper of an Hashtbl, it has nothing to do with > environnement. That's not true. It is a wrapper, but it does have something to do with the environment. The constructor creates a hashtbl from a Unix.environment string array. And the environment method translates the hashtbl back into a Unix.environment string array. The object provides data hiding and simplified syntax. > Why not simply : > > module Env > -------------- > type t = (string , string ) Hashtbl.t > > val get : t -> string -> string option > val default : t -> def:string -> env:string -> string > > val process : unit -> t > val dup : t -> t > val to_array : t -> string array > ------------------ I don't really understand the need for the process and dup functions. But you'd probably need a create function: val create : string array -> t And don't forget put: val put string -> string -> unit I chose to include an object for a reason though. I wanted to add additional functions that modified the existing process environment, as well as functions to modify a local fabricated environment. I didn't want to do that with two separate modules, and different function names would have been awkward. Plus, I like the data hiding that objects provide. The above module you have defined is only good for a local fabricated environment. Is there a severe performance penalty to wrapping a hashtbl in the way that my object does? I'm just trying to understand your aversion to it. I like objects. They're clean. > Now if you want to use object syntax for any container (Hashtbl, list, > array....) you might want to write an OO wrapper on Enums : > > class 'a enum : 'a Enum.t -> > object > ... > end I don't understand how I could wrap a Hashtbl with Enums. I guess I need to read up on them. -- Jesse Guardiani, Systems Administrator WingNET Internet Services, P.O. Box 2605 // Cleveland, TN 37320-2605 423-559-LINK (v) 423-559-5145 (f) http://www.wingnet.net |