From: Christoph Bauer <c_bauer@informatik.uni-kl.de>
To: OCaml List <caml-list@inria.fr>
Subject: [Caml-list] how to define a property list
Date: Fri, 23 Apr 2004 19:56:54 +0200 [thread overview]
Message-ID: <m31xmeefx5.fsf@diebuntekuh.de> (raw)
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
next reply other threads:[~2004-04-23 17:50 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-04-23 17:56 Christoph Bauer [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m31xmeefx5.fsf@diebuntekuh.de \
--to=c_bauer@informatik.uni-kl.de \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).