* Very very short question on objects
@ 1999-10-25 9:46 Christian RINDERKNECHT
1999-10-25 19:31 ` William Chesters
1999-10-25 20:33 ` Emmanuel CHAILLOUX
0 siblings, 2 replies; 3+ messages in thread
From: Christian RINDERKNECHT @ 1999-10-25 9:46 UTC (permalink / raw)
To: caml-list
Hi everybody,
I'm getting started with OCaml objects and, trying to compose
functional updates, I ran into something like the following
toy example:
Objective Caml version 2.02
# class x =
object
method private f = ()
method g = {< >} # f
end
;;
class x : object method f : unit method g : unit end
#
Method [f] is not private... Shouldn't be this an error?
Best regards,
Christian
-----------------------------------------------------------------------
Christian Rinderknecht Phone +33 (0)1 60 76 44 43
Institut National des Télécommunications Fax +33 (0)1 60 76 47 11
Département Logiciels Réseaux (LOR) WWW
9, Rue Charles Fourier, F-91011 Évry Cedex
^ permalink raw reply [flat|nested] 3+ messages in thread
* Very very short question on objects
1999-10-25 9:46 Very very short question on objects Christian RINDERKNECHT
@ 1999-10-25 19:31 ` William Chesters
1999-10-25 20:33 ` Emmanuel CHAILLOUX
1 sibling, 0 replies; 3+ messages in thread
From: William Chesters @ 1999-10-25 19:31 UTC (permalink / raw)
To: caml-list
Christian RINDERKNECHT writes:
> Method [f] is not private... Shouldn't be this an error?
It's deliberate: from node3.html#ss:private-methods in the html docs:
Private methods can be made public in a subclass. [...] One
could think that a private method should remain private in a
subclass. However, it since the method is visible in a subclass,
it is always possible pick it's code and define a method of the
same name that run that code, [...]
In other words, ocaml's `private' is more like Java's `protected', and
afair Java's behaviour here agrees with ocaml's.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Very very short question on objects
1999-10-25 9:46 Very very short question on objects Christian RINDERKNECHT
1999-10-25 19:31 ` William Chesters
@ 1999-10-25 20:33 ` Emmanuel CHAILLOUX
1 sibling, 0 replies; 3+ messages in thread
From: Emmanuel CHAILLOUX @ 1999-10-25 20:33 UTC (permalink / raw)
To: caml-list
Hi Christian,
> Hi everybody,
>
> I'm getting started with OCaml objects and, trying to compose
> functional updates, I ran into something like the following
> toy example:
>
> Objective Caml version 2.02
>
> # class x > object
> method private f = ()
> method g = {< >} # f
> end
> ;;
> class x : object method f : unit method g : unit end
> #
>
> Method [f] is not private... Shouldn't be this an error?
Because you add a type constraint as follow :
$ ocaml
Objective Caml version 2.02
class t = object(self :'a)
constraint 'a = < f : unit; ..> (* from {<>}#f *)
method private f = ()
method g = {<>}#f
end;;
class t : object method f : unit method g : unit end
If you have in the same class declaration, a method f with the attribute private, and the same method f without it, the
last definition wins.
# class d = object method private f = print_string "private" method f = print_string "public" end;;
class d : object method f : unit end
# let q = new d;;
val q : d = <obj>
# q#f;;
public- : unit = ()
including the case where the last declaration is private.
# class e = object method f = print_string "public" method private f = print_string "private" end;;
class e : object method f : unit end
# let i = new e;;
val i : e = <obj>
# i#f;;
private- : unit = ()
So the last declaration wins, includind a "private" declaration.
The other examples show some type constraints :
(* don't copy the instance to keep private method *)
# class y = object(self) method private f = () method g = self#f end;;
class y : object method private f : unit method g : unit end
(* It's same that your example *)
# class z = object(self) method private f = () method g = (Oo.copy self)#f end;;
class z : object method f : unit method g : unit end
(* Another case to add a type constraint (for the parameter o) *)
# class u o = object(self:'a) method private f = () method g (o : 'a) = o#f end;;
class u : 'b -> object ('a) method f : unit method g : 'a -> unit end
(* to see an inherited private method *)
# class v = object(self) inherit y as super method f = super#f end;;
class v : object method f : unit method g : unit end
If you want to simulate the attribute "private", you can use a functional instance variable as follows :
# class p (h : 'b) = object (self : 'a)
constraint 'b = 'a -> unit
val f = h
method g = f({<>})
end;;
class p : ('a -> unit) -> object ('a) val f : 'a -> unit method g : unit end
# let h x = ignore(Oo.copy x); ();;
val h : < .. > -> unit = <fun>
# let ip = new p h;;
val ip : p = <obj>
# ip#g;;
- : unit = ()
> Best regards,
>
> Christian
Best regards,
Emmanuel...
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1999-10-26 6:52 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-10-25 9:46 Very very short question on objects Christian RINDERKNECHT
1999-10-25 19:31 ` William Chesters
1999-10-25 20:33 ` Emmanuel CHAILLOUX
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox