* Static Function in Class
@ 2009-05-18 3:27 Vincent Cheval
2009-05-18 4:21 ` [Caml-list] " Jacques Garrigue
[not found] ` <4A10F3DB.9080007@yahoo.it>
0 siblings, 2 replies; 3+ messages in thread
From: Vincent Cheval @ 2009-05-18 3:27 UTC (permalink / raw)
To: caml-list
Hi,
I have a question about Object in Ocaml. I wonder if it's possible to create static function in the definition of a object in Ocaml. Here is a small exemple :
Assume that you have this class definition:
#class test (n:int) =
# object
# val x = n
# method get_x = x
# end;;
#
#let equal (t_1:test) (t_2:test) = t_1#x = t_2#x;;
This class and the function are well defined but I would like not to use the method "get_x" and define my class like that :
#class test (n:int) =
# object
# val x = n
# method equal (t_1:test) (t_2:test) = t_1#x = t_2#x
# end;;
If we were on Java or C++, i should use Static in front of the declaration of "equal". So my question is : Is it possible to do the same thing in OCaml ?
Thank You very much... it's my first mail on this mailing list so I hope it's not a boring question.
Vincent Cheval
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Static Function in Class
2009-05-18 3:27 Static Function in Class Vincent Cheval
@ 2009-05-18 4:21 ` Jacques Garrigue
[not found] ` <4A10F3DB.9080007@yahoo.it>
1 sibling, 0 replies; 3+ messages in thread
From: Jacques Garrigue @ 2009-05-18 4:21 UTC (permalink / raw)
To: vincheval; +Cc: caml-list
From: Vincent Cheval <vincheval@wanadoo.fr>
> I have a question about Object in Ocaml. I wonder if it's possible
> to create static function in the definition of a object in
> Ocaml. Here is a small exemple :
>
> Assume that you have this class definition:
>
> #class test (n:int) =
> # object
> # val x = n
> # method get_x = x
> # end;;
> #
> #let equal (t_1:test) (t_2:test) = t_1#x = t_2#x;;
>
> This class and the function are well defined but I would like not to
> use the method "get_x" and define my class like that :
>
> #class test (n:int) =
> # object
> # val x = n
> # method equal (t_1:test) (t_2:test) = t_1#x = t_2#x
> # end;;
>
> If we were on Java or C++, i should use Static in front of the
> declaration of "equal". So my question is : Is it possible to do the
> same thing in OCaml ?
Actually, a static method is not really a method, but just a way to
use classes as modules. OCaml already as modules independently of
classes, so this is not relevant: just use a normal function outside
of the class.
Looking more carefully about your 2nd example, it happens to be
impossible to encode it directly in ocaml, but for a different reason:
there is no way to access fields directly from outside an object. So
if you write a function outside of the class, it will not be able to
use x. The standard way to define such functions without exposing the
value of x is to add a method get_x, but give the returned value an
abstract type:
module Test : sig
type x_t
class test : int ->
object
method get_x : x_t
method print : unit
end
val equal : test -> test -> bool
end = struct
type x_t = int
class test (n : int) =
object
val x = n
method get_x = x
method print = print_int x
end
let equal t1 t2 = t1#get_x = t2#get_x
end
(I added another method, because using an object without any method is
not meaningful)
Another approach, if you don't want to inherit from test, it to use
private row variables:
module Test : sig
type test = private < print : unit; .. >
val create : int -> test
val equal : test -> test -> bool
end = struct
class test (n : int) =
object
val x = n
method get_x = x
method print = print_int x
end
let create = new test
let equal t1 t2 = t1#get_x = t2#get_x
end
Jacques Garrigue
^ permalink raw reply [flat|nested] 3+ messages in thread
[parent not found: <4A10F3DB.9080007@yahoo.it>]
end of thread, other threads:[~2009-05-18 9:25 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-18 3:27 Static Function in Class Vincent Cheval
2009-05-18 4:21 ` [Caml-list] " Jacques Garrigue
[not found] ` <4A10F3DB.9080007@yahoo.it>
2009-05-18 9:25 ` Vincent Cheval
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox