Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Object Attribute Accessor Syntax Extension
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-03-23 (20:01)
From: Will M. Farr <farr@M...>
Subject: Object Attribute Accessor Syntax Extension
Hash: SHA1


I would like to learn more about camlp4, so I thought I would undertake 
a simple project, but I would like some comment on my ideas first (in 
the hope that my project would be useful to others, too).  It has 
always irked me that when defining a class which is essentially an 
extensible data-container, one has to specify the data accessor 
functions in addition to the data values:

class body (m : float) (qq : float array) (pp : float array) =
   val mutable m = m
   val q = qq
   val p = pp
   val mutable f = Array.make (Array.length pp) 0.0
   method q = q
   method p = p
   method m = m
   method set_m mm = m <- mm
   method set_f ff = f <- ff
   (* etc *)

I think it would be nice to have accessor functions defined 
automatically for simple datatypes (sort of a simplified version of the 
:accessor q properties of a data member in CLOS -- Ruby does something 
like this with its :attr_accessor a,b,c notes in the class definiton, 
too, and Python probably has something similar).  The sytax I was 
thinking of using was:

class body (m : float) (qq : float array) (pp : float array) =
   val mutable accessor m = m
   val reader q = qq
   val reader p = pp
   val writer f = Array.make (Array.length pp) 0.0
   (* etc *)

If anyone would like to comment on this idea (because you hate the 
syntax, or love the syntax, or think it's been done before, or think 
it's a bad idea, etc) before I go ahead and write it, let me know.

Version: GnuPG v1.4.0 (Darwin)