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 ocaml.org.

Browse thread
Base class "constructor" being called multiple times?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: "ocaml_beginners"::[] Base class "constructor" being called multiple times?
On Tue, Jul 22, 2003 at 08:29:41AM -0700, james woodyatt wrote:
> Here's another clue to your issue:
[...]
> In this code, the 'base' and 'derived' classes have the same class 
> type.

This is obviously just a stripped down example. The real program
is too large to post.

I changed the code to read:

----------------------------------------------------------------------
class base =
let t = 0 in
object (self)
val obj = t
method obj = obj
end

class derived =
object (self)
inherit base
method foo = () (* dummy method *)
end

class contains =
object (self)
inherit derived

method add : 'a. (#derived as 'a) -> unit =
fun w -> Printf.printf "contains: %d\n" w#obj
end
----------------------------------------------------------------------

which compiles, thanks.

But is there a way to write the add method without the closure?

I tried various things like:

method (add : 'a. (#derived as 'a) -> unit) w =
Printf.printf "contains: %d\n" w#obj
method add w : 'a. (#derived as 'a) -> unit =
Printf.printf "contains: %d\n" w#obj
method add : 'a. (#derived as 'a) -> unit w =
Printf.printf "contains: %d\n" w#obj

but all give syntax errors.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
MONOLITH is an advanced framework for writing web applications in C, easier
than using Perl & Java, much faster and smaller, reusable widget-based arch,
database-backed, discussion, chat, calendaring:
http://www.annexia.org/freeware/monolith/