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: james woodyatt <jhw@w...>
Subject: Re: "ocaml_beginners"::[] Base class "constructor" being called multiple times?
On Tuesday, Jul 22, 2003, at 08:21 US/Pacific, Richard Jones wrote:
> On Tue, Jul 22, 2003 at 04:38:14PM +0200, Remi Vanicat wrote:
>> Richard Jones <rich@a...> writes:
>>
>>> I want the contains#add method to have the following signature:
>>>
>>> #derived -> unit
>>>
>>> meaning that it can only be applied to class 'derived' (and
>>> subclasses). It would be a mistake to call add with a 'base'.
>>
>> You should reread the ocaml documentation, and more precisely the
>> sections 3.9 and 3.10
>
> The documentation is extremely dense and very unclear on this
> issue. My latest attempt is:
>
> method add w : #derived -> unit = Printf.printf "contains: %d\n" 
> w#obj
>
> but the explicit type definition doesn't seem to apply to the method
> call. Moving around the type def just gives syntax errors.
>
> Any attempt to use the 'a. ... syntax just results in a syntax error.

Here's another clue to your issue:

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

class derived =
object (self)
inherit base
end

class contains =
object (self)
inherit derived

method add (w : #derived) = Printf.printf "contains: %d\n" w#obj
end
----------------------------------------------------------------------

In this code, the 'base' and 'derived' classes have the same class 
type. So, I can just dispense with the 'derived' class. Here is the 
transcript from the top-level that shows how to do what you want:

# class base =
let t = 0 in
object(self)
val obj = t
method obj = obj
end;;
class base : object val obj : int method obj : int end
# class contains =
object(self)
inherit base
method add: 'a. (#base as 'a) -> unit =
fun w -> Printf.printf "obj=%d\n" w#obj
end;;
class contains :
object val obj : int method add : #base -> unit method obj : int end


-- 
j h woodyatt <jhw@w...>
that's my village calling... no doubt, they want their idiot back.