caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: vincheval@wanadoo.fr
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Static Function in Class
Date: Mon, 18 May 2009 13:21:25 +0900 (JST)	[thread overview]
Message-ID: <20090518.132125.13768397.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <9493703.159904.1242617278198.JavaMail.www@wwinf1e35>

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


  reply	other threads:[~2009-05-18  4:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-18  3:27 Vincent Cheval
2009-05-18  4:21 ` Jacques Garrigue [this message]
     [not found] ` <4A10F3DB.9080007@yahoo.it>
2009-05-18  9:25   ` [Caml-list] " Vincent Cheval

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090518.132125.13768397.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=vincheval@wanadoo.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).