--- In ocaml_beginners%40yahoogroups.com">ocaml_beginners
yahoogroups.com, Robert Roessler <robertr
...>
wrote:
>
> mo.deeq wrote:
> > > I wondered if anyone had an idea of how to get around needing a
> > > hashtable in which all the keys are strings, but, the values
may be of
> > > any type. In effect some thing along the lines of:
> > >
> > > val mutable table : (string, 'a) ExtLib.Hashtbl.t
> > >
> > >
> > > the reason being that i'm writing an interpreter were variables are
> > > defined and assigned as in the language, i.e.
> > >
> > > int a = 1
> > > char b = "b"
> > > ...
> > >
> > > and so on. I would like to therefore keep a table of the values
held
> > > by various variables while my interpreter does its job.
> >
> > Sorry, forgot to mention, so far i'm using an object called
> > assignedVar, defined as:
> >
> > class assignedVar ( v_typ:string) (name:string ) (val:string)
> >
> > where i'm keeping everything as a string and casting based on the the
> > type as specified by 'v_typ'..
> >
> > I was hoping for a simpler and more efficient mechanism.
>
> If your needs are that simple (your v_typ values are known in
> advance), how about something *like* (your cases would be different)
>
> type prop_tbl = (string, [`Str of string | `Fun of unit -> string])
> Hashtbl.t
>
> which creates a "properties" table whose values can be either strings
> or functions returning strings, which can then be used by [e.g.]
>
> let (prop_tab: prop_tbl) = Hashtbl.create 53
>
> Then just do a match when you pull these out...
>
> Robert Roessler
> robertr
...
> http://www.rftp.com
>
ingenious method Robert, but sadly i allow users to define their own
types ..however, i'm sure i will find that definition somewhere 
deeq
.