List Info

Thread: Re: "ocaml_beginners"::[] Re: mixed value hashtable




Re: "ocaml_beginners"::[] Re: mixed value hashtable
country flaguser name
United States
2007-06-05 16:26:49

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%40rftp.com">robertrrftp.com
http://www.rftp.com

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: mixed value hashtable
country flaguser name
United States
2007-06-05 16:37:42

--- In ocaml_beginners%40yahoogroups.com">ocaml_beginnersyahoogroups.com, Robert Roessler <robertr...>
wrote:
&gt;
> mo.deeq wrote:
&gt; > > 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
&gt; > >
>; > >
>; > > 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&quot;
> > > ...
> > >
>; > > 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
&gt; > 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.]
&gt;
> let (prop_tab: prop_tbl) = Hashtbl.create 53
>
> Then just do a match when you pull these out...
&gt;
> 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

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )