caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] how to define a property list
@ 2004-04-23 17:56 Christoph Bauer
  2004-04-23 18:00 ` Alain.Frisch
  2004-04-23 18:06 ` Richard Jones
  0 siblings, 2 replies; 6+ messages in thread
From: Christoph Bauer @ 2004-04-23 17:56 UTC (permalink / raw)
  To: OCaml List

Hi List,

I want to create a tree. This tree should be a very simple accessible
for algorithms and easy to understand. The nodes in the tree can have
different properties of different types. 

An example of such a tree is the DOM-tree in JavaScript. 

There are several ways how to design such a tree.

  1.) Using Records. Each node is a records, and the
      records contain a field for each possible property.

Maybe a better approach is a type like this

type ('a,'b) node = { <common fields>; mutable properties : 'a * 'b list }

The list `properties' should be a key-value list. A Lisp-like alist would
be perfect. Unfortunately the type 'b isn't fix. It can be string or int
or something completely different.

  2a) Using dynamic typing.
       type property_value = Int of int | String of String.
       type 'a node = { <common fields>; mutable properties : 'a * property_value list }
     
     If this is the solution, I'll better switch to Scheme, which has better support
     for dynamic types ;-)
       
  2b) Add for each type a property list to node.

      type 'a node = { 
         <common fields>; 
         mutable int_properties : 'a * int list 
         mutable string_properties : 'a * string list 
         ...
      }

     This is my preferred solution so far.

  3) Using OOP.  (I'm not sure, whether OOP in general is good or bad.)

     class node =
     object end;;
    
     class ['a, 'b] sub_node a b=
     object(s)
        inherit node
        val mutable property1 : 'a = a ;
        val mutable property2 : 'b = b;
        method property1 = property1;
        method set_property1 = property1
        method property2 = property2;
        method set_property2 = property2
     end;;
     (* lot of typing for nothing so far*)

      let a = new sub_node 1 "a" :> node;;
      
   Question: How can I now access property1?
       
Thanks for comments,
Christoph Bauer

-- 
beginfig(1)u=3cm;draw fullcircle scaled 2u;x0=x1=y1=x2=y3=0;-y0=y2=x3=1u;
filldraw z0..{left}z1{left}..z2{curl 1}..z3..z0..cycle;def t(expr p)=fullcircle
scaled .25u shifted(0,p*u);enddef;unfill t(.5);fill t(-.5);endfig;bye

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-04-24  8:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-23 17:56 [Caml-list] how to define a property list Christoph Bauer
2004-04-23 18:00 ` Alain.Frisch
2004-04-23 18:51   ` Christoph Bauer
2004-04-24  8:15     ` Christoph Bauer
2004-04-23 18:06 ` Richard Jones
2004-04-23 19:02   ` Christoph Bauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).