From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id AD511BB84 for ; Tue, 9 May 2006 00:59:30 +0200 (CEST) Received: from smtp16.wanadoo.fr (smtp16.wanadoo.fr [193.252.23.89]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k48MxUMC026149 for ; Tue, 9 May 2006 00:59:30 +0200 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf1601.wanadoo.fr (SMTP Server) with ESMTP id 19C037000087 for ; Tue, 9 May 2006 00:59:28 +0200 (CEST) Received: from wwinf1626 (wwinf1626 [172.22.147.100]) by mwinf1601.wanadoo.fr (SMTP Server) with ESMTP id 16CDB7000084; Tue, 9 May 2006 00:59:28 +0200 (CEST) X-ME-UUID: 20060508225928934.16CDB7000084@mwinf1601.wanadoo.fr Message-ID: <13757673.1147129168085.JavaMail.www@wwinf1626> From: yoann padioleau Reply-To: padator@wanadoo.fr To: David Teller , caml-list@yquem.inria.fr Subject: Re: [Caml-list] OO design Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [193.54.76.34] X-Wum-Nature: EMAIL-NATURE X-WUM-FROM: |~| X-WUM-TO: |~||~| X-WUM-REPLYTO: |~| Date: Tue, 9 May 2006 00:59:28 +0200 (CEST) X-Miltered: at concorde with ID 445FCD52.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ocaml:01 rewriting:01 statically:01 ocaml:01 model:01 subclassing:01 run-time:01 titi:01 caml-list:01 functions:01 lisp:01 func:01 func:01 defining:02 checking:02 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.8 required=5.0 tests=DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST autolearn=disabled version=3.0.3 > Which brings us to a question : how do you enforce protocols in OCaml ? > > Say, is there a "good" way of rewriting file-related operations so that, say, > ProtocolUnix.read and ProtocolUnix.write *statically* only accept opened > files, and in addition, ProtocolUnix.write only accepts files which have been > opened with write priviledges ? Have different types, in_channel and out_channel. But ocaml already have this. The problem is that when you close a channel, ocaml does not warn you if you try to read from a close channel. > > I mean, there are manners of checking this with, say, model checking tools. In > the specific case of file management, I guess we can do it with a little bit > of simple subclassing, but I assume there's a large run-time penalty for this > extra bit of checking, due to the management of objects by OCaml. Has anyone > attempted to determine how well this scales up ? Or explored other options ? Using higher order functions. Instead of having a open/read/close sequence protocol that you must follow, enforce such a protocol by defining a higher order function let's say with_open_out_file that do all that for you under the hood. with_open_out_file "/tmp/test.txt" (fun write_func -> (* you can call write_func that do the writing *) write_func "toto"; write_func "titi"; ); let with_open_out_file file f = let chan = open_out file in let read_func = output_string chan in try ( f read_func; close_out chan; ) with x -> close_out chan; raise x Note how the channel is automatically closed for you. This technique is used in Lisp library I think.