From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 1B76DBBAF for ; Mon, 18 May 2009 06:22:16 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAM5+EEqCNhAB/2dsb2JhbADOO4QBBQ X-IronPort-AV: E=Sophos;i="4.41,208,1241388000"; d="scan'208";a="27887083" Received: from kurims.kurims.kyoto-u.ac.jp ([130.54.16.1]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 18 May 2009 06:22:14 +0200 Received: from localhost (orion [130.54.16.5]) by kurims.kurims.kyoto-u.ac.jp (8.13.8/8.13.8) with ESMTP id n4I4M5fN017862; Mon, 18 May 2009 13:22:06 +0900 (JST) Date: Mon, 18 May 2009 13:21:25 +0900 (JST) Message-Id: <20090518.132125.13768397.garrigue@math.nagoya-u.ac.jp> To: vincheval@wanadoo.fr Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Static Function in Class From: Jacques Garrigue In-Reply-To: <9493703.159904.1242617278198.JavaMail.www@wwinf1e35> References: <9493703.159904.1242617278198.JavaMail.www@wwinf1e35> X-Mailer: Mew version 4.2 on Emacs 22.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; ocaml:01 ocaml:01 val:01 val:01 sig:01 bool:01 struct:01 sig:01 bool:01 struct:01 abstract:01 encode:01 caml-list:01 functions:01 inherit:01 From: Vincent Cheval > 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