English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] how to define a property list
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-04-23 (17:50)
From: Christoph Bauer <c_bauer@i...>
Subject: [Caml-list] how to define a property 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=
        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
     (* 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